Technology Programming

THook Delphi Class with Source Code



Code submitted by Jens Borrisholt. Text by Zarko Gajic.
By Jens: Hooks, I’ve seen a lot of people trying to make a clean solution for hooking messages in an application. So I decided some time ago to implement hooks as a class, with nice events and stuff :)

Hook.pas makes it possible to assign a method pointer to a procedure pointer (with some help from assembler).

For example: if you want to trap ALL keystrokes in your application - simply declare an instance of TKeyboardHook, assign an event handler for OnPreExecute or OnPostExecute, or both.

Set you KeyboadHook active (KeyboardHook.Active := True) and you are out and running ..

On Windows Hooks

Here's what the Windows API guide has to say on hooks:
A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure.

Put shortly, a hook is a function you can create as part of a dll or your application to monitor the 'goings on' inside the Windows operating system.

The idea is to write a function that is called every time a certain event in windows occurs - for example when a user presses a key on the keyboard or moves the mouse.

For a more in depth introduction to hooks, take a look at What Windows hooks are and how to use them within a Delphi application.

Hooking mechanism relies on Windows messages and callback functions.

Types of Hooks

Different hook types enable an application to monitor a different aspect of the system's message-handling mechanism.
For example:
You can use the WH_KEYBOARD hook to monitor keyboard input posted to a message queue;
You can use the WH_MOUSE hook to monitor mouse input posted to a message queue;
You can a WH_SHELL hook procedure when the shell application is about to be activated and when a top-level window is created or destroyed.

Hooks.pas

The hooks.pas unit defines several hook types:
  • TCBTHook - called before activating, creating, destroying, minimizing, maximizing, moving, or sizing a window; before completing a system command; before removing a mouse or keyboard event from the system message queue; before setting the input focus; or before synchronizing with the system message queue.
  • TDebugHook - called before calling hook procedures associated with any other hook in the system
  • TGetMessageHook - enables an application to monitor messages about to be returned by the GetMessage or PeekMessage function
  • TJournalPlaybackHook - enables an application to insert messages into the system message queue.
  • TJournalRecordHook - enables you to monitor and record input events (to record a sequence of mouse and keyboard events to play back later by using the WH_JOURNALPLAYBACK Hook).
  • TKeyboardHook - enables an application to monitor message traffic for WM_KEYDOWN and WM_KEYUP messages.
  • TMouseHook - enables you to monitor mouse messages about to be returned by the GetMessage or PeekMessage function.
  • TLowLevelKeyboardHook - enables you to monitor keyboard input events about to be posted in a thread input queue.
  • TLowLevelMouseHook - enables you to monitor mouse input events about to be posted in a thread input queue.

TKeyboardHook example

To show you how to use the hooks.pas, here's a section of the keyboard hook demo application:
Download hooks.pas + demo application

uses hooks, ....var   KeyboardHook: TKeyboardHook; .... //MainForm's OnCreate event handlerprocedure TMainForm.FormCreate(Sender: TObject) ; begin   KeyboardHook := TKeyboardHook.Create;   KeyboardHook.OnPreExecute := KeyboardHookPREExecute;   KeyboardHook.Active := True; end;//handles KeyboardHook's OnPREExecuteprocedure TMainForm.KeyboardHookPREExecute(Hook: THook; var Hookmsg: THookMsg) ; var   Key: Word; begin   //Here you can choose if you want to return   //the key stroke to the application or not   Hookmsg.Result := IfThen(cbEatKeyStrokes.Checked, 1, 0) ;   Key := Hookmsg.WPARAM;  Caption := Char(key) ; end; Ready, set, hook :)
SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*