procedure TForm1.FormDestroy(Sender: TObject);
begin
UnHookWindowsHookEx(KeyHook);
end;
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;
begin
if nCode > -1 then begin
if lParam >= 0 then begin
if Form1.Timer1.Enabled then begin
Form1.Button1.Caption := '성공하셨네요';
Form1.Timer1.Enabled := False;
end
else begin
Form1.Button1.Caption := '0.5초안에 눌러요';
Form1.Timer1.Enabled := True;
end;
end;
Result := 0;
end
else Result := CallNextHookEx(KeyHook, nCode, wParam, lParam);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Caption := '0.5초안에 눌러요';
Timer1.Enabled := True;
end;
키보드후킹을 써보았습니다.
타이머 interval은 500주고요.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
KeyHook: HHOOK;
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult; stdcall;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
KeyHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, 0, GetCurrentThreadID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnHookWindowsHookEx(KeyHook);
end;
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;
begin
if nCode > -1 then begin
if lParam >= 0 then begin
if Form1.Timer1.Enabled then begin
Form1.Button1.Caption := '성공하셨네요';
Form1.Timer1.Enabled := False;
end
else begin
Form1.Button1.Caption := '0.5초안에 눌러요';
Form1.Timer1.Enabled := True;
end;
end;
Result := 0;
end
else Result := CallNextHookEx(KeyHook, nCode, wParam, lParam);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Caption := '0.5초안에 눌러요';
Timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Button1.Caption := '왜안누르는데';
Timer1.Enabled := False;
end;
end.