Delphi Source Code
Search engine
HOME Components Tutorials Add Trick Links Contacts
ActiveX Components Database Files Forms Graphic Internet/Lan Math Miscellaneous Multimedia Printing Strings System Information Windows

Delphi source code for Windows >> Trap mouse events outside of my application

Category: Windows
Title: Trap mouse events outside of my application
Date added: 15.03.2006
Hits: 27187

unit Unit1;
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, AppEvnts, StdCtrls;
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    Button_StartJour: TButton;
    Button_StopJour: TButton;
    ListBox1: TListBox;
procedure ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
procedure Button_StartJourClick(Sender: TObject);
procedure Button_StopJourClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
    { Private declarations }
    FHookStarted : Boolean;
    { Public declarations }
  Form1: TForm1;
{$R *.dfm}
  JHook: THandle;

function JournalProc(Code, wParam: Integer; var EventStrut: TEventMsg): Integer; stdcall;
  Char1: PChar;
  s: string;
  {this is the JournalRecordProc}
  Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
  {the CallNextHookEX is not really needed for journal hook since it it not
  really in a hook chain, but it's standard for a Hook}

  if Code < 0 then Exit;
  {you should cancel operation if you get HC_SYSMODALON}
  if Code = HC_SYSMODALON then Exit;
  if Code = HC_ACTION then
    The lParam parameter contains a pointer to a TEventMsg
    structure containing information on
    the message removed from the system message queue.

    s := '';
    if EventStrut.message = WM_LBUTTONUP then
      s := 'Left Mouse UP at X pos ' +
        IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);
    if EventStrut.message = WM_LBUTTONDOWN then
      s := 'Left Mouse Down at X pos ' +
        IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);
    if EventStrut.message = WM_RBUTTONDOWN then
      s := 'Right Mouse Down at X pos ' +
        IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);
    if (EventStrut.message = WM_RBUTTONUP) then
      s := 'Right Mouse Up at X pos ' +
        IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);
    if (EventStrut.message = WM_MOUSEWHEEL) then
      s := 'Mouse Wheel at X pos ' +
        IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);
    if (EventStrut.message = WM_MOUSEMOVE) then
      s := 'Mouse Position at X:' +
        IntToStr(EventStrut.paramL) + ' and Y: ' + IntToStr(EventStrut.paramH);
    if s <> '' then
       Form1.ListBox1.ItemIndex :=  Form1.ListBox1.Items.Add(s);

procedure TForm1.Button_StartJourClick(Sender: TObject);
  if FHookStarted then
    ShowMessage('Mouse is already being Journaled, can not restart');
  JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0);
  {SetWindowsHookEx starts the Hook}
  if JHook > 0 then
    FHookStarted := True;
    ShowMessage('No Journal Hook availible');

procedure TForm1.Button_StopJourClick(Sender: TObject);
  FHookStarted := False;
  JHook := 0;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
  {the journal hook is automaticly camceled if the Task manager
  (Ctrl-Alt-Del) or the Ctrl-Esc keys are pressed, you restart it
  when the WM_CANCELJOURNAL is sent to the parent window, Application}

  Handled := False;
  if (Msg.message = WM_CANCELJOURNAL) and FHookStarted then
    JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, 0, 0);

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  {make sure you unhook it if the app closes}
  if FHookStarted then

Related Delphi Source Code:
Delphi Tricks
Delphi Tricks
For any problems or recommendations about Delphi Tricks site, please

feel free to contact us on that e-mail:
If you want to advertise on the site use that e-mail:

You can freely use or modify these Delphi source codes for non-commercial use. We are not responsible of any damages that can be caused by the utilisation of that source codes.

Copyright © 2006-2010 AVSoftware Company. All rights reserved.
Hide IP tricks