[PT Run] Add setting to use centralized keyboard hook (#13557)

* [PT Run] Add setting to use centralized hook

* Add UI separator
This commit is contained in:
Jaime Bernardo
2021-10-01 14:59:52 +01:00
committed by GitHub
parent 55054f1fa6
commit 84b2ae3e8f
11 changed files with 124 additions and 29 deletions

View File

@@ -112,7 +112,7 @@ namespace PowerLauncher
_settingsVM = new SettingWindowViewModel();
_settings = _settingsVM.Settings;
_settings.UsePowerToysRunnerKeyboardHook = e.Args.Contains("--centralized-kb-hook");
_settings.StartedFromPowerToysRunner = e.Args.Contains("--started-from-runner");
_stringMatcher = new StringMatcher();
StringMatcher.Instance = _stringMatcher;

View File

@@ -100,6 +100,11 @@ namespace PowerLauncher
_settings.Hotkey = openPowerlauncher;
}
if (_settings.UseCentralizedKeyboardHook != overloadSettings.Properties.UseCentralizedKeyboardHook)
{
_settings.UseCentralizedKeyboardHook = overloadSettings.Properties.UseCentralizedKeyboardHook;
}
if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults)
{
_settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults;

View File

@@ -88,9 +88,15 @@ namespace PowerLauncher.ViewModel
// Allow OOBE to call PowerToys Run.
NativeEventWaiter.WaitForEventLoop(Constants.PowerLauncherSharedEvent(), OnHotkey);
if (_settings.StartedFromPowerToysRunner)
{
// Allow runner to call PowerToys Run from the centralized keyboard hook.
NativeEventWaiter.WaitForEventLoop(Constants.PowerLauncherCentralizedHookSharedEvent(), OnCentralizedKeyboardHookHotKey);
}
_settings.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(PowerToysRunSettings.Hotkey))
if (e.PropertyName == nameof(PowerToysRunSettings.Hotkey) || e.PropertyName == nameof(PowerToysRunSettings.UseCentralizedKeyboardHook))
{
Application.Current.Dispatcher.Invoke(() =>
{
@@ -741,26 +747,33 @@ namespace PowerLauncher.ViewModel
Log.Info("Unregistering previous low level key handler", GetType());
}
_globalHotKeyVK = hotkey.Key;
_globalHotKeyFSModifiers = VKModifiersFromHotKey(hotkey);
if (NativeMethods.RegisterHotKey(hwnd, _globalHotKeyId, _globalHotKeyFSModifiers, _globalHotKeyVK))
if (_settings.StartedFromPowerToysRunner && _settings.UseCentralizedKeyboardHook)
{
// Using global hotkey registered through the native RegisterHotKey method.
_globalHotKeyHwnd = hwnd;
_usingGlobalHotKey = true;
Log.Info("Registered global hotkey", GetType());
return;
Log.Info("Using the Centralized Keyboard Hook for the HotKey.", GetType());
}
Log.Warn("Registering global shortcut failed. Will use low-level keyboard hook instead.", GetType());
// Using fallback low-level keyboard hook through HotkeyManager.
if (HotkeyManager == null)
else
{
HotkeyManager = new HotkeyManager();
}
_globalHotKeyVK = hotkey.Key;
_globalHotKeyFSModifiers = VKModifiersFromHotKey(hotkey);
if (NativeMethods.RegisterHotKey(hwnd, _globalHotKeyId, _globalHotKeyFSModifiers, _globalHotKeyVK))
{
// Using global hotkey registered through the native RegisterHotKey method.
_globalHotKeyHwnd = hwnd;
_usingGlobalHotKey = true;
Log.Info("Registered global hotkey", GetType());
return;
}
_hotkeyHandle = HotkeyManager.RegisterHotkey(hotkey, action);
Log.Warn("Registering global shortcut failed. Will use low-level keyboard hook instead.", GetType());
// Using fallback low-level keyboard hook through HotkeyManager.
if (HotkeyManager == null)
{
HotkeyManager = new HotkeyManager();
}
_hotkeyHandle = HotkeyManager.RegisterHotkey(hotkey, action);
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception)
@@ -817,6 +830,14 @@ namespace PowerLauncher.ViewModel
}
*/
private void OnCentralizedKeyboardHookHotKey()
{
if (_settings.StartedFromPowerToysRunner && _settings.UseCentralizedKeyboardHook)
{
OnHotkey();
}
}
private void OnHotkey()
{
Application.Current.Dispatcher.Invoke(() =>