unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
ButtonHook: TButton;
ButtonUnhook: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure ButtonHookClick(Sender: TObject);
procedure ButtonUnhookClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
hookHandle: HHook;function KeyboardCallback(code, wParam, lParam: Integer): HRESULT; stdcall;implementation{$R *.dfm}procedure TForm1.ButtonHookClick(Sender: TObject);
var
threadId: Integer;
begin
threadId := StrToInt(edit1.Text);
hookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardCallback, HInstance, threadId);
if (hookHandle = 0) then
ShowMessage('hook error');
end;procedure TForm1.ButtonUnhookClick(Sender: TObject);
begin
if (hookHandle <> 0) then begin
UnhookWindowsHookEx(hookHandle);
hookHandle := 0;
end;
end;function KeyboardCallback(code, wParam, lParam: Integer): HRESULT; stdcall;
begin
if wParam = WM_KEYDOWN then begin
if lParam = VK_F1 then begin
Form1.Memo1.Text := 'okokok';
end;
end;
Result := CallNextHookEx(hookHandle, code, wParam, lParam);
end;end.
=====================
以上是我得代码,晕死,搞了1天没搞明白,进行了HOOK之后在被HOOK窗体按F1就提示程序错误。哪位高手给个代码呀?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
ButtonHook: TButton;
ButtonUnhook: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure ButtonHookClick(Sender: TObject);
procedure ButtonUnhookClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
hookHandle: HHook;function KeyboardCallback(code, wParam, lParam: Integer): HRESULT; stdcall;implementation{$R *.dfm}procedure TForm1.ButtonHookClick(Sender: TObject);
var
threadId: Integer;
begin
threadId := StrToInt(edit1.Text);
hookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardCallback, HInstance, threadId);
if (hookHandle = 0) then
ShowMessage('hook error');
end;procedure TForm1.ButtonUnhookClick(Sender: TObject);
begin
if (hookHandle <> 0) then begin
UnhookWindowsHookEx(hookHandle);
hookHandle := 0;
end;
end;function KeyboardCallback(code, wParam, lParam: Integer): HRESULT; stdcall;
begin
if wParam = WM_KEYDOWN then begin
if lParam = VK_F1 then begin
Form1.Memo1.Text := 'okokok';
end;
end;
Result := CallNextHookEx(hookHandle, code, wParam, lParam);
end;end.
=====================
以上是我得代码,晕死,搞了1天没搞明白,进行了HOOK之后在被HOOK窗体按F1就提示程序错误。哪位高手给个代码呀?
解决方案 »
- delphi vista下如何弹出UAC
- 如何取出当前数据集所操作的表名及此表中的主键个数及主键字段名称呢?
- 各位大侠,请教如何实现将程序最小化到系统托盘中?
- Delphi语句
- 全文检索的实现方法?
- 如何用SQL语句更改表名,怎么用EXEC SP-rename table1,table2,object不行,在线等
- 在Delphi中使用MonthCalender组件,可否在程序中设置某几日的前景色或背景色?
- 如何调用“打开方式”对话框
- 怎样判断float类型的小数点后的位数?例如:123.45,那么位数为2.
- DELPHI与COM!!!
- 高分求 dragdrop 4.0 组件。 一个消失的的经典…
- KeyPress 事件中
具体用法请看这里:
blog.csdn.net/linzhengqun
找钩子及其应用。
这一行代码,用了HInstance,后面一个参数就要设置为0 ,如果后面一个参数设置为险程ID,那么前一个参数就要设置成0下面是正确的
hookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardCallback, 0, GetCurrentThreadID);
hookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardCallback, HInstance, 0);
hModIdentifies the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process. dwThreadIdSpecifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads.