方法一:
function LowLevelKeyboardProc(nCode:integer;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;
type
KBDLLHOOKSTRUCT=record
vkCode:DWORD;
scanCode:DWORD;
flags:DWORD;
time:DWord;
dwExtraInfo:dword;
end;
var
fFlag:BOOL;
p:^KBDLLHOOKSTRUCT;
begin
result:=0;
fFlag := false;
p:=Pointer(LPARAM);
if (ncode=HC_ACTION) then
begin
case wparam of
WM_KEYDOWN,
WM_SYSKEYDOWN,
WM_KEYUP,
WM_SYSKEYUP:
fFlag :=(p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin)or (p.vkCode = VK_apps);
end;
end;
if fFlag = true then
Result:=1;
if ncode <> 0 then
Result := CallNextHookEX(0,ncode,wparam,lparam);
end;
procedure TForm1.Button1Click(Sender: TObject);
const
WH_KEYBOARD_LL=13;
begin
setwindowshookexw(WH_KEYBOARD_LL,LowlevelKeyboardproc,hinstance,0);
end;
方法二:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure WndProc(var Message: TMessage); override;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
wm_sock:integer;
hHandle:HWND;
function LowLevelKeyboardProc(nCode:integer;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;
type
KBDLLHOOKSTRUCT=record
vkCode:DWORD;
scanCode:DWORD;
flags:DWORD;
time:DWord;
dwExtraInfo:dword;
end;
var
fFlag:BOOL;
p:^KBDLLHOOKSTRUCT;
begin
result:=0;
fFlag := false;
p:=Pointer(LPARAM);
if (ncode=HC_ACTION) then
begin
case wparam of
WM_KEYDOWN,
WM_SYSKEYDOWN,
WM_KEYUP,
WM_SYSKEYUP:
fFlag :=(p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin)or (p.vkCode = VK_apps);
end;
end;
if fFlag = true then
begin
PostMessage(hHandle,WM_SOCK,0,0);
Result:=1;
end;
if ncode <> 0 then
Result := CallNextHookEX(0,ncode,wparam,lparam);
end;
procedure TForm1.Button1Click(Sender: TObject);
const
WH_KEYBOARD_LL=13;
begin
wm_sock := 0;
wm_sock := RegisterWindowMessage('wm_sock');
// showmessage(inttostr(wm_sock));
hHandle := Self.Handle;
setwindowshookexw(WH_KEYBOARD_LL,LowLevelKeyboardProc,hinstance,0);
end;
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg=WM_SOCK then
begin
showmessage('用户按下win键');
end;
inherited;
end;
end.