From f76b78ebc4eac523ac314e99cbb02a6ff1e1b5c0 Mon Sep 17 00:00:00 2001 From: Cody Simonds <36618932+CodySimonds65@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:27:19 -0600 Subject: [PATCH] fix: only hold shortcut keys while FourFold is in front RegisterHotKey took a bound chord such as Shift+Space from every other app for as long as FourFold ran. The chords are now released when FourFold loses focus and registered again when it comes back, and plain-key shortcuts no longer fire on keys pressed in another app. --- .../MainWindow.xaml.cs | 12 ++++-- .../Services/RawInputListener.cs | 2 +- .../Services/WindowsGlobalHotkeyRegistrar.cs | 39 ++++++++++++++++++- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/FourFoldAccountManager.Desktop/MainWindow.xaml.cs b/src/FourFoldAccountManager.Desktop/MainWindow.xaml.cs index 0befa83..ed3a794 100644 --- a/src/FourFoldAccountManager.Desktop/MainWindow.xaml.cs +++ b/src/FourFoldAccountManager.Desktop/MainWindow.xaml.cs @@ -53,6 +53,7 @@ public partial class MainWindow : Window private readonly BuiltInPluginSet _plugins; private HwndSource? _windowSource; private GlobalShortcutRegistry? _shortcuts; + private WindowsGlobalHotkeyRegistrar? _hotkeys; private RawInputListener? _rawInput; private readonly PlainKeyShortcutMatcher _plainKeys = new(); // Recording a shortcut must not reveal overlays, toggle dividers, or change a live run. @@ -186,8 +187,12 @@ private void MainWindow_SourceInitialized(object? sender, EventArgs e) _windowSource.AddHook(MainWindow_HwndSourceHook); _rawInput = RawInputListener.TryRegister(windowHandle, HandleObservedInput); + _hotkeys = new WindowsGlobalHotkeyRegistrar(windowHandle); + _hotkeys.SetSuspended(!RawInputListener.IsThisAppInFront()); + Application.Current.Activated += (_, _) => _hotkeys?.SetSuspended(false); + Application.Current.Deactivated += (_, _) => _hotkeys?.SetSuspended(true); _shortcuts = new GlobalShortcutRegistry(new PlainKeyRoutingRegistrar( - new WindowsGlobalHotkeyRegistrar(windowHandle), plainKeysAvailable: _rawInput is not null)); + _hotkeys, plainKeysAvailable: _rawInput is not null)); } private IntPtr MainWindow_HwndSourceHook( @@ -263,9 +268,10 @@ private void HandleObservedInput(ushort input, bool isDown) return; } - // A mouse shortcut only acts while FourFold is in front: scrolling in another app must not switch tabs. + // Shortcuts only act while FourFold is in front: keys pressed in another app or game are theirs. if (_plainKeys.KeyDown(input) is { } action && - (GlobalHotkeyChord.IsMouseInput(input) ? RawInputListener.IsThisAppInFront() : !IsTypingInFourFold())) + RawInputListener.IsThisAppInFront() && + (GlobalHotkeyChord.IsMouseInput(input) || !IsTypingInFourFold())) { HandleGlobalShortcut(action); } diff --git a/src/FourFoldAccountManager.Desktop/Services/RawInputListener.cs b/src/FourFoldAccountManager.Desktop/Services/RawInputListener.cs index cbfde8d..4708fd7 100644 --- a/src/FourFoldAccountManager.Desktop/Services/RawInputListener.cs +++ b/src/FourFoldAccountManager.Desktop/Services/RawInputListener.cs @@ -59,7 +59,7 @@ public void SetMouseEnabled(bool enabled) } } - // Raw input reaches this window whichever app is in front; mouse shortcuts only act while FourFold is. + // Raw input reaches this window whichever app is in front; shortcuts only act while FourFold is. public static bool IsThisAppInFront() => GetWindowThreadProcessId(GetForegroundWindow(), out var processId) != 0 && processId == Environment.ProcessId; diff --git a/src/FourFoldAccountManager.Desktop/Services/WindowsGlobalHotkeyRegistrar.cs b/src/FourFoldAccountManager.Desktop/Services/WindowsGlobalHotkeyRegistrar.cs index 001c5a8..fd8213b 100644 --- a/src/FourFoldAccountManager.Desktop/Services/WindowsGlobalHotkeyRegistrar.cs +++ b/src/FourFoldAccountManager.Desktop/Services/WindowsGlobalHotkeyRegistrar.cs @@ -11,6 +11,8 @@ internal sealed class WindowsGlobalHotkeyRegistrar : IGlobalHotkeyRegistrar private const uint ModifierNoRepeat = 0x4000; private readonly IntPtr _windowHandle; + private readonly Dictionary _registered = []; + private bool _suspended; public WindowsGlobalHotkeyRegistrar(IntPtr windowHandle) { @@ -46,10 +48,43 @@ public bool TryRegister(int id, GlobalHotkeyChord chord) modifiers |= ModifierShift; } - return RegisterHotKey(_windowHandle, id, modifiers, chord.VirtualKey); + if (!RegisterHotKey(_windowHandle, id, modifiers, chord.VirtualKey)) + { + return false; + } + + // While suspended the trial registration only proves Windows would accept the chord. + if (_suspended) + { + _ = UnregisterHotKey(_windowHandle, id); + } + + _registered[id] = (modifiers, chord.VirtualKey); + return true; } - public void Unregister(int id) => _ = UnregisterHotKey(_windowHandle, id); + public void Unregister(int id) + { + _registered.Remove(id); + _ = UnregisterHotKey(_windowHandle, id); + } + + // RegisterHotKey takes its chord from every app, so the chords are only held while FourFold is in front. + public void SetSuspended(bool suspended) + { + if (suspended == _suspended) + { + return; + } + + _suspended = suspended; + foreach (var (id, chord) in _registered) + { + _ = suspended + ? UnregisterHotKey(_windowHandle, id) + : RegisterHotKey(_windowHandle, id, chord.Modifiers, chord.Key); + } + } [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)]