Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Keyboard HookGo to William Egge's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Creating a keyboard hook in your application
Product:
Delphi 5.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
10/02/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet keyboard keystroke catch TKeyboardHook KeyboardProc
Times Scored:
2
Visits:
8384
Uploader: William Egge
Company: Eggcentric
Reference: http://www.eggcentric.com/KeyboardHook.htm
Component Download: http://www.eggcentric.com/download/KeyboardHook.zip
 
Question/Problem/Abstract:
You want to catch keystrokes. Your main form "keypreview=true" only works when a control has focus. How do you catch keystrokes? Included below is a component you can drop on any form to catch keystrokes.
Answer:



Catching a keyboard stroke for your application requires a call to the windows api function SetWindowsHookEx.

Using this function will allow you to specify a callback procedure for the keystrokes.

You have 2 options, you can make a system wide hook or a hook for a particular thread.

If you make a system wide hook or specify a thread that exists in a different process then you must use a seperate DLL.

In this artical I will only provide source for your applications main thread, therefore is not a system wide hook.

Install the below component, drop it on your form, then assign the OnCallback event.

You can look into windows help for "KeyboardProc" and it will explain the parameters.

Here is an implementation I am using in a production application that responds to the space bar:

procedure TForm_LibraryImageSelect.KeyboardHook1Callback(code, wparam,
  lparam: Integer);
begin
  if (wParam = VK_SPACE) and ((lparam and (1 shl 30)) > 0) then
    SendMoveNextLibImage(dNext);
end;


Good luck,

Here is the source for the component:

----------------------------------------

unit KeyboardHook;
{
  By William Egge
  Sep 20, 2002
  egge@eggcentric.com
  http://www.eggcentric.com

  This code may be used/modified however you wish.
}

interface

uses
  Windows, Classes;

type
  TCallbackThunk = packed record
    POPEDX: Byte;
    MOVEAX: Byte;
    SelfPtr: Pointer;
    PUSHEAX: Byte;
    PUSHEDX: Byte;
    JMP: Byte;
    JmpOffset: Integer;
  end;

  // See windows help on KeyboardProc
  // Or press F1 while your cursor is on "KeyboardProc"
  TKeyboardCallback =
      procedure(code: Integer; wparam: WPARAM; lparam: LPARAM) of object;

  TKeyboardHook = class(TComponent)
  private
    { Private declarations }
    FHook: HHook;
    FThunk: TCallbackThunk;
    FOnCallback: TKeyboardCallBack;
    function CallBack(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT
        stdcall;
    procedure SetOnCallback(const Value: TKeyboardCallBack);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property OnCallback: TKeyboardCallBack read FOnCallback write SetOnCallback;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('EggMisc', [TKeyboardHook]);
end;

{ TKeyboardHook }

function TKeyboardHook.CallBack(code: Integer; wparam: WPARAM;
  lparam: LPARAM): LRESULT;
begin
  if Code < 0 then
    Result:= CallNextHookEx(FHook, Code, wparam, lparam)
  else
  begin
    if Assigned(FOnCallback) then
      FOnCallback(Code, wParam, lParam);
    Result:= 0;
  end;
end;

constructor TKeyboardHook.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FThunk.POPEDX:= $5A;
  FThunk.MOVEAX:= $B8;
  FThunk.SelfPtr:= Self;
  FThunk.PUSHEAX:= $50;
  FThunk.PUSHEDX:= $52;
  FThunk.JMP:= $E9;
  FThunk.JmpOffset:= Integer(@TKeyboardHook.Callback)-Integer(@FThunk.JMP)-5;
  FHook:= SetWindowsHookEx(WH_KEYBOARD, TFNHookProc(@FThunk), 0, MainThreadID);
end;

destructor TKeyboardHook.Destroy;
begin
  UnhookWindowsHookEx(FHook);
  inherited;
end;

procedure TKeyboardHook.SetOnCallback(const Value: TKeyboardCallBack);
begin
  FOnCallback := Value;
end;

end.






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Preventing key-strokes
    Tommy Andersen (Nov 15 2004 8:48PM)

If you want to prevent a key-stroke or combination, define WH_KEYBOARD_LL = 13.

Instead of calling SetWindowsHookEx with WH_KEYBOARD, call it with WH_KEYBOARD_LL .

Whenever you encounter a key-stroke you don't want, just set the result to 1 instead of calling CallNextHookEx .


Respond

system wide hook
    Eber Irigoyen (Oct 3 2002 2:27AM)

there's an article here on how to implement the system wide keyboard hook, it works like a dream... you need to do some work for your specific needs though... but the hard part is all done (the DLL and the implementation of the call(back))

system wide keyboard hook

salu2

EberSys
Respond

RE: system wide hook
William Egge (Oct 3 2002 2:53AM)

Cool Thanks----------------------------------------
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Maes
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)