请问一个简单问题,我这个键盘Hook为什么不能捕捉其他窗口的键盘输入?(100分)

  • 主题发起人 主题发起人 xhyph
  • 开始时间 开始时间
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.
 
给你一个例子把。
Dll里的:
library HKTest;

uses
HKProc in 'HKProc.pas';

exports
EnableHotKeyHook,
DisableHotKeyHook;

begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.

Unit里的:
unit HookU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';
function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';

procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('Keyboard Hook 启动, 按下 Ctrl + B 将叫出「记事本」');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('Keyboard Hook 已解除!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
label1.Caption:='单击“启动热键”按钮'+#13+
'无论你何时按下Ctrl+B键,'+#13'都会接收到响应';
end;

end.

Unit2里的
unit HookU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';
function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';

procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('Keyboard Hook 启动, 按下 Ctrl + B 将叫出「记事本」');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('Keyboard Hook 已解除!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
label1.Caption:='单击“启动热键”按钮'+#13+
'无论你何时按下Ctrl+B键,'+#13'都会接收到响应';
end;

end.
Unit4里的
unit HKProc;

interface

uses
Windows, Messages;

var
hNextHookProc: HHook;
procSaveExit: Pointer;


function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
function EnableHotKeyHook: BOOL; export;
function DisableHotKeyHook: BOOL; export;
procedure HotKeyHookExit; far;

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) and //(lparam and $80000000= 0) -->down <>0 --> up;
(GetKeyState(vk_control) <0) and (wParam = Ord('B')) then
begin
Result := 1;
WinExec('Notepad.exe', sw_Normal); // 记事本
end;
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.
有点乱,不过可以看得懂!!!!:)

 
你的例子就是我的那个例子,不过我象让键盘按下的信息储存在数据库里,怎么做?
 
接受答案了.
 
后退
顶部