Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/FourFoldAccountManager.Desktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal sealed class WindowsGlobalHotkeyRegistrar : IGlobalHotkeyRegistrar
private const uint ModifierNoRepeat = 0x4000;

private readonly IntPtr _windowHandle;
private readonly Dictionary<int, (uint Modifiers, uint Key)> _registered = [];
private bool _suspended;

public WindowsGlobalHotkeyRegistrar(IntPtr windowHandle)
{
Expand Down Expand Up @@ -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)]
Expand Down
Loading