作者:流浪者时空 | 来源:互联网 | 2023-09-06 14:20
unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Qt;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
//function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
Form1: TForm1;
hook: HHOOK; {定义一个钩子句柄}
implementation
{$R *.dfm}
//var
{实现键盘钩子回调函数}
function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;stdcall;
begin
// ShowMessage('aaabbb '+inttostr(wParam)+' '+inttostr(lParam));
if (wParam = 119) and(lParam>0) then
begin
ShowMessage('aaa '+inttostr(wParam)+' '+inttostr(lParam)+' '+inttostr(nCode));
Beep; {每拦截到字母 A 会发声}
end;
Result := CallNextHookEx(hook, nCode, wParam, lParam);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
hook := SetWindowsHookEx(WH_KEYBOARD, @KeyHook, 0, GetCurrentThreadID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(hook);
end;
procedure TForm1.Button1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//Key_Enter
end;
end.