Skip to content

Commit d3d1f6f

Browse files
committed
Fix Unexpected Intercept result when multiple plugin register the global hotkey event
1 parent 1c60b8e commit d3d1f6f

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lPa
7979
{
8080
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
8181
}
82-
return (IntPtr)1;
82+
return (IntPtr)(-1);
8383
}
8484

8585
public static void Dispose()

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,21 @@ public interface IPublicAPI
104104
/// Fired after global keyboard events
105105
/// if you want to hook something like Ctrl+R, you should use this event
106106
/// </summary>
107+
[Obsolete("Unable to Retrieve correct return value")]
107108
event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;
109+
110+
/// <summary>
111+
/// Register a callback for Global Keyboard Event
112+
/// </summary>
113+
/// <param name="callback"></param>
114+
public void RegisterGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback);
115+
116+
/// <summary>
117+
/// Remove a callback for Global Keyboard Event
118+
/// </summary>
119+
/// <param name="callback"></param>
120+
public void RemoveGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback);
121+
108122

109123
/// <summary>
110124
/// Fuzzy Search the string with the given query. This is the core search mechanism Flow uses

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM
4040
_settingsVM = settingsVM;
4141
_mainVM = mainVM;
4242
_alphabet = alphabet;
43-
GlobalHotkey.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
43+
GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback;
4444
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
4545
}
4646

@@ -190,25 +190,35 @@ public void OpenDirectory(string DirectoryPath, string FileName = null)
190190
Arguments = FileName is null ?
191191
explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath) :
192192
explorerInfo.FileArgument.Replace("%d", DirectoryPath).Replace("%f",
193-
Path.IsPathRooted(FileName) ? FileName : Path.Combine(DirectoryPath, FileName))
193+
Path.IsPathRooted(FileName) ? FileName : Path.Combine(DirectoryPath, FileName))
194194
};
195195
explorer.Start();
196196
}
197197

198198
public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;
199199

200+
private readonly List<Func<int, int, SpecialKeyState, bool>> _globalKeyboardHandlers = new();
201+
202+
public void RegisterGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Add(callback);
203+
public void RemoveGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Remove(callback);
204+
200205
#endregion
201206

202207
#region Private Methods
203208

204209
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
205210
{
211+
var continueHook = true;
206212
if (GlobalKeyboardEvent != null)
207213
{
208-
return GlobalKeyboardEvent((int)keyevent, vkcode, state);
214+
continueHook = GlobalKeyboardEvent((int)keyevent, vkcode, state);
215+
}
216+
foreach (var x in _globalKeyboardHandlers)
217+
{
218+
continueHook &= x((int)keyevent, vkcode, state);
209219
}
210220

211-
return true;
221+
return continueHook;
212222
}
213223

214224
#endregion

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void Init(PluginInitContext context)
300300
{
301301
this.context = context;
302302
_settings = context.API.LoadSettingJsonStorage<Settings>();
303-
context.API.GlobalKeyboardEvent += API_GlobalKeyboardEvent;
303+
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
304304
}
305305

306306
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)

0 commit comments

Comments
 (0)