Delphi - 键盘上的Hook WIN键,用于显示我的开始菜单

时间:2017-08-08 17:25:35

标签: delphi

我将SetWindowsHookEx()WH_KEYBOARD_LL一起使用。

我想挂钩 WIN 键来打开我的应用程序,但问题是如果我按 WIN + R 等,那么这个键快捷键不起作用。

我的来源受到此代码的启发:

https://github.com/MasterOfChaos/Chaoslauncher/blob/master/Source/Plugins/Chaosplugin/Hotkeys.pas

请帮帮我。如何使用键盘上的 WIN 键打开我的应用程序,仍然可以使用 WIN + R WIN + E WIN + L 等?

1 个答案:

答案 0 :(得分:0)

这对我很好:

const
WH_KEYBOARD_LL   =   13;
LLKHF_ALTDOWN    =   $00000020;
LLKHF_INJECTED   =   $00000010;
KEYEVENTF_KEYDOWN = 0;

type
tagKBDLLHOOKSTRUCT   =   record
    vkCode:   DWORD;
    scanCode:   DWORD;
    flags:   DWORD;
    time:   DWORD;
    dwExtraInfo:   DWORD;
  end;
KBDLLHOOKSTRUCT   =   tagKBDLLHOOKSTRUCT;
LPKBDLLHOOKSTRUCT   =   ^KBDLLHOOKSTRUCT;
PKBDLLHOOKSTRUCT   =   ^KBDLLHOOKSTRUCT;

var
hhkLowLevelKybd:   HHOOK;
prev:PKBDLLHOOKSTRUCT;
KeysPressed: tstringlist;



function LowLevelKeyBoardProc(nCode:   Integer;   awParam:   WPARAM;   
alParam:   LPARAM):   LRESULT;   stdcall;
var
fEatKeyStroke:   Boolean;
act:   PKBDLLHOOKSTRUCT;

begin
fEatKeystroke   :=   False;


if (nCode = HC_ACTION) then
begin
    case awParam  of
        WM_KEYDOWN,
        WM_SYSKEYDOWN,
        WM_KEYUP,
        WM_SYSKEYUP:
            begin
            act   :=   PKBDLLHOOKSTRUCT(alParam);

            // start sezereme jen pokud je zmacnute jen jedno tlacitko a je to Win
            if KeysPressed.Count=1 then begin if act^.vkCode = VK_LWIN then fEatKeystroke := True; end;

            // pokud zmacnke tlacitko pridame do listboxu, v kombinaci at tam jsou dva stisky
            if awParam=WM_KEYDOWN then
               if KeysPressed.IndexOf(char(act^.vkCode))=-1 then KeysPressed.Add(char(act^.vkCode)); // prida do listboxu

            // pokud uz neni zmacknute smazeme
            if awParam=WM_KEYUP then
               KeysPressed.Clear;


            end; // case
            end;//case
end;//nCode




if fEatKeyStroke then
   Result := 1
else
   Result := CallNextHookEx(hhkLowLevelKybd, nCode, awParam, alParam);




end;





procedure InstallHook;
begin
KeysPressed := tstringlist.Create;
if hhkLowLevelKybd <> 0 then exit;
hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc,   
hInstance,   0);
end;

procedure UninstallHook;
begin
KeysPressed.Free;
if hhkLowLevelKybd = 0 then exit;
UnhookWindowsHookEx(hhkLowLevelKybd);
hhkLowLevelKybd := 0;
end;




procedure TFrmMain.FormCreate(Sender: TObject);
begin
InstallHook;
end;



procedure TFrmMain.FormDestroy(Sender: TObject);
begin
UninstallHook;
end;