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.Application Visible) then
begin
MainForm.Tray.HideApplicat ion;
// 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
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.Application
begin
MainForm.Tray.HideApplicat
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it should be
function KeyboardHookProc (code, wParam, lParam : integer) : integer; stdcall;
instead of
function KeyboardHookProc (Code: Integer; WParam: Word; LParam: LongInt) : LongInt;
function KeyboardHookProc (code, wParam, lParam : integer) : integer; stdcall;
instead of
function KeyboardHookProc (Code: Integer; WParam: Word; LParam: LongInt) : LongInt;
It should indeed.. :-)
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}