[PowerToys Run] Use global HotKey instead of low level keyboard hook (#13114)

* [PowerToys Run] Register global HotKey

Using low level keyboard hooks caused focus issues when invoking
PowerToys Run. Using a global HotKey solves this issue.

* Properly unregister hotkey on dispose

* fix spellchecker errors
This commit is contained in:
Jaime Bernardo
2021-09-08 18:39:51 +01:00
committed by GitHub
parent 2c58bdbfb2
commit 5963294b04
8 changed files with 189 additions and 50 deletions

View File

@@ -31,6 +31,18 @@ namespace PowerLauncher.Helper
[DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[DllImport("user32")]
internal static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32")]
internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
@@ -86,6 +98,34 @@ namespace PowerLauncher.Helper
}
}
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
internal enum HOTKEY_MODIFIERS : uint
{
ALT = 0x0001,
CONTROL = 0x0002,
SHIFT = 0x0004,
WIN = 0x0008,
NOREPEAT = 0x4000,
CHECK_FLAGS = 0x000F, // modifiers to compare between keys.
}
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
internal enum SW : int
{
HIDE = 0x0000,
SHOWNORMAL = 0x0001,
SHOWMINIMIZED = 0x0002,
SHOWMAXIMIZED = 0x0003,
SHOWNOACTIVATE = 0x0004,
SHOW = 0x0005,
MINIMIZE = 0x0006,
SHOWMINNOACTIVE = 0x0007,
SHOWNA = 0x0008,
RESTORE = 0x0009,
SHOWDEFAULT = 0x000A,
FORCEMINIMIZE = 0x000B,
}
internal enum WM
{
NULL = 0x0000,
@@ -185,6 +225,8 @@ namespace PowerLauncher.Helper
NCMOUSELEAVE = 0x02A2,
HOTKEY = 0x0312,
DWMCOMPOSITIONCHANGED = 0x031E,
DWMNCRENDERINGCHANGED = 0x031F,
DWMCOLORIZATIONCOLORCHANGED = 0x0320,