Link to home
Start Free TrialLog in
Avatar of kapot
kapot

asked on

Keyboard hook question

Hi,

I made a "global" keyboard hook using Delphi. It works fine.

But how can I clear the key that got hooked ?

My function below will be called continuously when I press the ESC key (hooked key).

function KeyboardHookProc (Code: Integer; WordParam: Word; LongParam: LongInt) : LongInt;
begin
  // ESC Key preseed ?
  if (WordParam = $1B) then
  begin
    if (MainForm.Tray.ApplicationVisible) then
    begin
       MainForm.Tray.HideApplication;
       // I need to clear the keyboard buffer here ! So this function will be called only ONCE. How ?
    end;
  end;
end;

I need a way to DISPOSE / REMOVE they "key" after it has been hooked.

Any idea ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1 more thing - if I'm not wrong, you should add 'stdcall' there:
function KeyboardHookProc (code, wParam, lParam : integer) : integer; stdcall;

About "So this function will be called only ONCE" - I already said that if it is keyboard hook, it will be called on each wm_keydown and each wm_keyup message (and these are different messages). I suppose that you need to process only one type of actions, i.e., pressing OR releasing key. You may check if key is pressed or released by checking bit nr.31 of lParam which specifies the transition state (0 if the key is being pressed and 1 if it is being released). Just from head:
if (wParam = $1B) and (lParam and (1 shl 31) = 0) then {key is being pressed}
About those "word" and "longint" - some "wrong" resources say that wParam is 'short parameter' (16bit), lParam is 'long parameter' (32bit), but if you take a look at MSDN (Microsoft Developer Network, http://msdn.microsoft.com/), you'll see that they both are 32bit parameters:

3) INT = 32-bit signed integer
2) typedef unsigned int UINT_PTR;
1) typedef UINT_PTR WPARAM;

3) LONG = 32-bit signed integer
2) typedef long LONG_PTR;
1) typedef LONG_PTR LPARAM;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it should be
function KeyboardHookProc (code, wParam, lParam : integer) : integer; stdcall;
instead of
function KeyboardHookProc   (Code: Integer;   WParam: Word;   LParam: LongInt) : LongInt;
Avatar of BedouinDN
BedouinDN

It should indeed.. :-)