X
xhyph
Unregistered / Unconfirmed
GUEST, unregistred user!
Dll文件是抄CSDN的
library KeyHookDll;
uses
KeyHookProc in 'KeyHookProc.pas',
SysUtils,
Classes,ADODB;
exports
EnableHotKeyHook,
DisableHotKeyHook,
SetDataSet;
{$R *.RES}
begin
hNextHookProc := 0;
// 以下这两列, 旨在确定 DLL UnLoad 时, 确定将 HOOK 解除
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.
KeyHookProc.pas
unit KeyHookProc;
interface
uses
Windows, Messages,Dialogs,sysutils,ADODB;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
DataCon:TADOQuery;
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
function EnableHotKeyHook: BOOL; export;
function DisableHotKeyHook: BOOL; export;
procedure HotKeyHookExit; far;
procedure SetDataSet(DataSet:TADOQuery);export;
//这个函数是用来将我的应用程序的ADOQuery赋值到这里的DataCon,
//让它接收到键盘按键后写入数据库
implementation
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测 Ctrl + B 组合键
if ((lParam and _KeyPressMask) = 0) then //(lparam and $80000000= 0) -->down <>0 --> up;
begin
DataCon.Insert;
DataCon.FieldByName('KeyName').AsString := IntToStr( wParam);
DataCon.Post;
ShowMessage(IntToStr( wParam));
//如果不添加数据库一切正常,但是有了以上的,就不能捕捉其他窗口的键盘 //消息,请问我该怎么办,如何实现我的目的,将键盘按键的消息侄一一写入数 //据库
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
end;
end;
procedure SetDataSet(DataSet:TADOQuery);export;
begin
DataCon:=DataSet;
end;
function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
end.
library KeyHookDll;
uses
KeyHookProc in 'KeyHookProc.pas',
SysUtils,
Classes,ADODB;
exports
EnableHotKeyHook,
DisableHotKeyHook,
SetDataSet;
{$R *.RES}
begin
hNextHookProc := 0;
// 以下这两列, 旨在确定 DLL UnLoad 时, 确定将 HOOK 解除
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.
KeyHookProc.pas
unit KeyHookProc;
interface
uses
Windows, Messages,Dialogs,sysutils,ADODB;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
DataCon:TADOQuery;
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
function EnableHotKeyHook: BOOL; export;
function DisableHotKeyHook: BOOL; export;
procedure HotKeyHookExit; far;
procedure SetDataSet(DataSet:TADOQuery);export;
//这个函数是用来将我的应用程序的ADOQuery赋值到这里的DataCon,
//让它接收到键盘按键后写入数据库
implementation
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测 Ctrl + B 组合键
if ((lParam and _KeyPressMask) = 0) then //(lparam and $80000000= 0) -->down <>0 --> up;
begin
DataCon.Insert;
DataCon.FieldByName('KeyName').AsString := IntToStr( wParam);
DataCon.Post;
ShowMessage(IntToStr( wParam));
//如果不添加数据库一切正常,但是有了以上的,就不能捕捉其他窗口的键盘 //消息,请问我该怎么办,如何实现我的目的,将键盘按键的消息侄一一写入数 //据库
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
end;
end;
procedure SetDataSet(DataSet:TADOQuery);export;
begin
DataCon:=DataSet;
end;
function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
end.