Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to catch "MOUSE CLICK/MOVE" in any place on screen ( IN or OUT form area / application) using MouseHookProc() - my way >:)

emailx45

Programmer
Jan 10, 2025
8
Code:
type
  TForm1 = class(TForm)
    Label2: TLabel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

(*  Winapi.Messages.pas
  WM_MOUSEFIRST       = $0200; 512
  WM_MOUSEMOVE        = $0200;
  WM_LBUTTONDOWN      = $0201; 513
  WM_LBUTTONUP        = $0202; 514
  WM_LBUTTONDBLCLK    = $0203; 515
  WM_RBUTTONDOWN      = $0204; 516
  WM_RBUTTONUP        = $0205; 517
  WM_RBUTTONDBLCLK    = $0206; 518
  WM_MBUTTONDOWN      = $0207; 519
  WM_MBUTTONUP        = $0208; 520
  WM_MBUTTONDBLCLK    = $0209; 521
  WM_MOUSEWHEEL       = $020A; 522

  HC_ACTION = 0;
*)
var
  FHookHandle: HHOOK;

function MyMouseClick(AwParam: wParam): string;
begin
  case AwParam of
    WM_MOUSEMOVE:
      result := '(Mouse Moving action)';
    WM_LBUTTONDOWN:
      result := '(Left Button Down)';
    WM_LBUTTONUP:
      result := '(Left Button Up)';
    WM_LBUTTONDBLCLK:
      result := '(Left Button Double-click)';
    WM_RBUTTONDOWN:
      result := '(Right Button Down)';
    WM_RBUTTONUP:
      result := '(Right Button Up)';
    WM_RBUTTONDBLCLK:
      result := '(Right Button Double-click)';
    WM_MBUTTONDOWN:
      result := '(Middle Button Down)';
    WM_MBUTTONUP:
      result := '(Middle Button Up)';
    WM_MBUTTONDBLCLK:
      result := '(Middle Button Double-click)';
    WM_MOUSEWHEEL:
      result := '(Mouse Wheel action)';
  else
    result := 'Others...';
  end;
end;

function MouseHookProc(nCode: integer; wParam: wParam; lParam: lParam): LRESULT; stdcall;
begin
  Form1.Label2.Caption := 'Time: ' + TimeToStr(Now);
  Form1.Label3.Caption := 'HC_ACTION = ' + HC_ACTION.ToString;
  Form1.Label4.Caption := 'nCode = ' + nCode.ToString;
  Form1.Label5.Caption := 'wParam = ' + wParam.ToString + ' ' + MyMouseClick(wParam);
  //
  if (HC_ACTION = 0) then
    begin
      case wParam of
        WM_LBUTTONDOWN:
          begin
            Form1.Label1.Caption := 'Mouse Left Clicked';
          end;
        WM_RBUTTONDOWN:
          begin
            Form1.Label1.Caption := 'Mouse Right Clicked';
          end;
      end;
    end;
  //
  result := CallNextHookEx(FHookHandle, nCode, wParam, lParam); // next process...
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // installing the "hook"
  FHookHandle := SetWindowsHookEx(WH_MOUSE_LL, @MouseHookProc, HInstance, 0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // removing the "hook" - DONT FORGET!
  UnhookWindowsHookEx(FHookHandle);
end;

initialization

ReportMemoryLeaksOnShutdown   := true;
FormatSettings.LongTimeFormat := 'hh:nn:ss.zzz';

end.

explorer_pKeJPUWKEV.gif
 

Part and Inventory Search

Sponsor

Back
Top