From d3d1f6faa75a6a182b847e6f1db0d2f4b51e2015 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Sat, 27 Nov 2021 14:26:20 -0600 Subject: [PATCH] Fix Unexpected Intercept result when multiple plugin register the global hotkey event --- .../Hotkey/GlobalHotkey.cs | 2 +- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 14 ++++++++++++++ Flow.Launcher/PublicAPIInstance.cs | 18 ++++++++++++++---- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs b/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs index 3ea8915f7..afbf58a3e 100644 --- a/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs +++ b/Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs @@ -79,7 +79,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey { return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam); } - return (IntPtr)1; + return (IntPtr)(-1); } public static void Dispose() diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 3abdaf01f..90679fc1c 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -104,7 +104,21 @@ namespace Flow.Launcher.Plugin /// Fired after global keyboard events /// if you want to hook something like Ctrl+R, you should use this event /// + [Obsolete("Unable to Retrieve correct return value")] event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent; + + /// + /// Register a callback for Global Keyboard Event + /// + /// + public void RegisterGlobalKeyboardCallback(Func callback); + + /// + /// Remove a callback for Global Keyboard Event + /// + /// + public void RemoveGlobalKeyboardCallback(Func callback); + /// /// Fuzzy Search the string with the given query. This is the core search mechanism Flow uses diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 562db43f1..0b475f3a5 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -40,7 +40,7 @@ namespace Flow.Launcher _settingsVM = settingsVM; _mainVM = mainVM; _alphabet = alphabet; - GlobalHotkey.hookedKeyboardCallback += KListener_hookedKeyboardCallback; + GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback; WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); } @@ -190,25 +190,35 @@ namespace Flow.Launcher Arguments = FileName is null ? explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath) : explorerInfo.FileArgument.Replace("%d", DirectoryPath).Replace("%f", - Path.IsPathRooted(FileName) ? FileName : Path.Combine(DirectoryPath, FileName)) + Path.IsPathRooted(FileName) ? FileName : Path.Combine(DirectoryPath, FileName)) }; explorer.Start(); } public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent; + private readonly List> _globalKeyboardHandlers = new(); + + public void RegisterGlobalKeyboardCallback(Func callback) => _globalKeyboardHandlers.Add(callback); + public void RemoveGlobalKeyboardCallback(Func callback) => _globalKeyboardHandlers.Remove(callback); + #endregion #region Private Methods private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state) { + var continueHook = true; if (GlobalKeyboardEvent != null) { - return GlobalKeyboardEvent((int)keyevent, vkcode, state); + continueHook = GlobalKeyboardEvent((int)keyevent, vkcode, state); + } + foreach (var x in _globalKeyboardHandlers) + { + continueHook &= x((int)keyevent, vkcode, state); } - return true; + return continueHook; } #endregion diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index f9fc239a5..bdc3d69ce 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -300,7 +300,7 @@ namespace Flow.Launcher.Plugin.Shell { this.context = context; _settings = context.API.LoadSettingJsonStorage(); - context.API.GlobalKeyboardEvent += API_GlobalKeyboardEvent; + context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent); } bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)