[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

@@ -8,7 +8,6 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using Microsoft.Win32.SafeHandles;
using Wox.Plugin.Logger;
@@ -17,32 +16,11 @@ namespace PowerLauncher.Helper
{
public static class EnvironmentHelper
{
private const string EnvironmentChangeType = "Environment";
private const string Username = "USERNAME";
private const string ProcessorArchitecture = "PROCESSOR_ARCHITECTURE";
private const string Path = "PATH";
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA1801:Review unused parameters", Justification = "Params are required for delegate signature requirements.")]
public static IntPtr ProcessWindowMessages(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
switch ((WM)msg)
{
case WM.SETTINGCHANGE:
string changeType = Marshal.PtrToStringUni(lparam);
if (changeType == EnvironmentChangeType)
{
Log.Info("Reload environment", typeof(EnvironmentHelper));
UpdateEnvironment();
handled = true;
}
break;
}
return IntPtr.Zero;
}
private static void UpdateEnvironment()
internal static void UpdateEnvironment()
{
// Username and process architecture are set by the machine vars, this
// may lead to incorrect values so save off the current values to restore.

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,