蔡
蔡成
Unregistered / Unconfirmed
GUEST, unregistred user!
我需要用一个DLL的程序来做键盘钩子,当它捕捉到键盘按键时,可将近键消息
传给在后台工作的主程序,再由后台工作的主程序将按键消息转换,部分按键
消息需传给当前的应用程序。
我写了一个DLL程序,是根据钱达智的例子改写出,但是只有主程序工作在
前台时才能收到DLL的消息,当主程序工作在后台时便收不到了。
我看过关于moues hook的例子,但那个例子与我的这种做法不太一样。
在此请教各位高手。
DLL文件如下:
unit HKProc;
interface
uses
Windows, Messages,Sysutils,classes;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
var
HWndHotKey : Thandle;
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;
if (IntToHex(wParam,2) = 'FF') then
begin
if ((lparam and _KeyPressMask) = 0 ) then
begin
HWndHotKey:= FindWindow('TMainForm', 'hotkey');
postmessage(HWnDHotKey,WM_keyDown,wParam,lParam);
Result:= 1;
end
else
begin
if (IntToHex(lparam and $80FF0000,8) = '80300000') then
begin
HWndHotKey:= FindWindow('TMainForm', 'hotkey');
postmessage(HWnDHotKey,WM_keyUp,wParam,lParam);
Result:= 1;
end;
end;
end;
end;
function EnableHotKeyHook: BOOL
export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,HInstance,0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL
export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
end.
还有,我编译这个程序可以正常通过,但在运行中会偶尔出现runtime 216错
误,请问问题在哪里?
传给在后台工作的主程序,再由后台工作的主程序将按键消息转换,部分按键
消息需传给当前的应用程序。
我写了一个DLL程序,是根据钱达智的例子改写出,但是只有主程序工作在
前台时才能收到DLL的消息,当主程序工作在后台时便收不到了。
我看过关于moues hook的例子,但那个例子与我的这种做法不太一样。
在此请教各位高手。
DLL文件如下:
unit HKProc;
interface
uses
Windows, Messages,Sysutils,classes;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
var
HWndHotKey : Thandle;
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;
if (IntToHex(wParam,2) = 'FF') then
begin
if ((lparam and _KeyPressMask) = 0 ) then
begin
HWndHotKey:= FindWindow('TMainForm', 'hotkey');
postmessage(HWnDHotKey,WM_keyDown,wParam,lParam);
Result:= 1;
end
else
begin
if (IntToHex(lparam and $80FF0000,8) = '80300000') then
begin
HWndHotKey:= FindWindow('TMainForm', 'hotkey');
postmessage(HWnDHotKey,WM_keyUp,wParam,lParam);
Result:= 1;
end;
end;
end;
end;
function EnableHotKeyHook: BOOL
export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,HInstance,0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL
export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
end.
还有,我编译这个程序可以正常通过,但在运行中会偶尔出现runtime 216错
误,请问问题在哪里?