1,183
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
KBDLLHOOKSTRUCT = record
vkCode: DWORD;
ScanCode: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Hook: HHOOK;
implementation
{$R *.dfm}
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
p: PKBDLLHOOKSTRUCT;
y: Integer;
begin
y:= 0;
if nCode = HC_ACTION then
begin
case WParam of
WM_KEYDOWN, WM_SYSKEYDOWN:
begin
p:= PKBDLLHOOKSTRUCT(Lparam);
if p^.vkCode=66 then
begin
y:=1;
keybd_event(Ord('C'), 0, 0, 0);
keybd_event(Ord('C'), 0, KEYEVENTF_KEYUP, 0);
end;
end;
WM_KEYUP, WM_SYSKEYUP:
begin
y:=1;
end;
end;
end;
if y = 1 then Result:= 1 else Result:= CallNextHookEx(hook,nCode,WParam,LParam);
end;
function SetHook: Boolean; stdcall;
begin
if hook <> 0 then
begin
Result:= False;
Exit;
end;
hook:= SetWindowsHookEx(13, @HookProc, HINSTANCE, 0);
Result:= hook <> 0;
end;
function DelHook: Boolean; stdcall;
begin
if hook <> 0 then
begin
UnhookWindowshookEx(hook);
hook:= 0;
end;
Result:= hook = 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetHook;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DelHook;
end;
end.