1
1211
Unregistered / Unconfirmed
GUEST, unregistred user!
我用钩子来监测键盘,来记录按下的是哪个键,可是记录结果总是把键记录了2次,这是为什么。
原代码如下,很简单的问题,可是我却不会,望大家指点。
unit mainfrm;
interface
uses
; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
; StdCtrls;
type
; TForm1 = class(TForm)
; ; procedure FormCreate(Sender: TObject);
; ; procedure FormDestroy(Sender: TObject);
end;
{the prototype for the new keyboard hook function}
function KeyboardHook(nCode: Integer; wParam: WPARAM;
; ; ; ; ; ; ; ; ; ; ; ; lParam: LPARAM): LResult; stdcall;
var
; Form1: TForm1;
; WinHook: HHOOK; ; ; // a handle to the keyboard hook function
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
; {install the keyboard hook function into the keyboard hook chain}
; WinHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, 0, GetCurrentThreadID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
; {remove the keyboard hook function from the keyboard hook chain}
; UnhookWindowsHookEx(WinHook);
end;
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;
var
; debugfile:textfile;
begin
; {if we can process the hook information...}
; ; if (nCode>-1) then
; ; assignfile(debugfile,'c:/test.txt');
; ; append(debugfile);
; ; if getkeystate(vk_return)<0 then
; ; begin
; ; ; write(debugfile,char(wparam))
; ; end
; ; else
; ; begin
; ; ; write(debugfile,char(wparam));
; ; ; closefile(debugfile);
; ; ; result:=0;
; ; end;
; if (nCode<=-1) then
; ; {we must pass the hook information to the next hook in the chain //setdoubleclicktime}
; ; Result := CallNextHookEx(WinHook, nCode, wParam, lParam);
end;
end.
原代码如下,很简单的问题,可是我却不会,望大家指点。
unit mainfrm;
interface
uses
; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
; StdCtrls;
type
; TForm1 = class(TForm)
; ; procedure FormCreate(Sender: TObject);
; ; procedure FormDestroy(Sender: TObject);
end;
{the prototype for the new keyboard hook function}
function KeyboardHook(nCode: Integer; wParam: WPARAM;
; ; ; ; ; ; ; ; ; ; ; ; lParam: LPARAM): LResult; stdcall;
var
; Form1: TForm1;
; WinHook: HHOOK; ; ; // a handle to the keyboard hook function
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
; {install the keyboard hook function into the keyboard hook chain}
; WinHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, 0, GetCurrentThreadID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
; {remove the keyboard hook function from the keyboard hook chain}
; UnhookWindowsHookEx(WinHook);
end;
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;
var
; debugfile:textfile;
begin
; {if we can process the hook information...}
; ; if (nCode>-1) then
; ; assignfile(debugfile,'c:/test.txt');
; ; append(debugfile);
; ; if getkeystate(vk_return)<0 then
; ; begin
; ; ; write(debugfile,char(wparam))
; ; end
; ; else
; ; begin
; ; ; write(debugfile,char(wparam));
; ; ; closefile(debugfile);
; ; ; result:=0;
; ; end;
; if (nCode<=-1) then
; ; {we must pass the hook information to the next hook in the chain //setdoubleclicktime}
; ; Result := CallNextHookEx(WinHook, nCode, wParam, lParam);
end;
end.