GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(domain): if multiple keybindings match user input, call the longer key combination
Lars Berger29bb802b7d55
diff --git a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs b/LarsWM.Infrastructure/WindowsApi/KeybindingService.csindex 09531bee..1cba89f4 100644--- a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs+++ b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs@@ -98,67 +98,52 @@ namespace LarsWM.Infrastructure.WindowsApivar pressedKey = inputEvent.Key;- Debug.WriteLine("Alt is down: " + ((GetKeyState(Keys.LMenu) & 0x8000) == 0x8000));+ Keybinding matchedKeybinding = null;- Debug.WriteLine("Shift is down: " + ((GetKeyState(Keys.LShiftKey) & 0x8000) == 0x8000));+ foreach (var keybinding in _registeredKeybindings)+ {+ var isMatch = keybinding.KeyCombination.All(key => key == pressedKey || IsKeyDown(key));- var keyboardState = GetCurrentKeyboardState();+ if (!isMatch)+ continue;- foreach (var key in Enum.GetValues(typeof(Keys)))- {- if (IsDown(keyboardState, (Keys)key))- Debug.WriteLine("Key is down: " + key);+ // Replace the keybinding to call if the key combination is longer.+ if (matchedKeybinding == null || keybinding.KeyCombination.Count() > matchedKeybinding.KeyCombination.Count())+ matchedKeybinding = keybinding;}- foreach (var keybinding in _registeredKeybindings)+ // Invoke the matched keybinding.+ if (matchedKeybinding != null){- var isMatch = keybinding.KeyCombination.All(key => IsDown(keyboardState, key) || key == pressedKey);+ matchedKeybinding.KeybindingProc();- if (isMatch)- {- keybinding.KeybindingProc();-- // Avoid forwarding the key input to other applications.- return new IntPtr(1);- }+ // Avoid forwarding the key input to other applications.+ return new IntPtr(1);}return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);}- private byte[] GetCurrentKeyboardState()- {- var keyboardState = new byte[256];- GetKeyboardState(keyboardState);- return keyboardState;- }-/// <summary>- /// Whether the specified key is down in a keyboard state snapshot.+ /// Whether the specified key is down./// </summary>- private bool IsDown(byte[] state, Keys key)- {- if ((int)key < 256) return IsDownRaw(state, key);- if (key == Keys.Alt) return IsDownRaw(state, Keys.LMenu) || IsDownRaw(state, Keys.RMenu);- if (key == Keys.Shift) return IsDownRaw(state, Keys.LShiftKey) || IsDownRaw(state, Keys.RShiftKey);- if (key == Keys.Control) return IsDownRaw(state, Keys.LControlKey) || IsDownRaw(state, Keys.RControlKey);- return false;- }-- private bool IsDownRaw(byte[] state, Keys key)+ private bool IsKeyDown(Keys key){- var virtualKeyCode = (int)key;+ if (key == Keys.Alt)+ return IsKeyDownRaw(Keys.LMenu) || IsKeyDownRaw(Keys.RMenu);- // if (virtualKeyCode < 0 || virtualKeyCode > 255)- // throw new ArgumentOutOfRangeException("key", key, "The value must be between 0 and 255.");+ if (key == Keys.Shift)+ return IsKeyDownRaw(Keys.LShiftKey) || IsKeyDownRaw(Keys.RShiftKey);- var keyState = state.ElementAtOrDefault(virtualKeyCode);+ if (key == Keys.Control)+ return IsKeyDownRaw(Keys.LControlKey) || IsKeyDownRaw(Keys.RControlKey);- // if (keyState == null)- // return false;+ return IsKeyDownRaw(key);+ }- // Get the high-order bit. If the high-order bit is 1, the key is down.- return keyState >> 7 == 1;+ private bool IsKeyDownRaw(Keys key)+ {+ return (GetKeyState(key) & 0x8000) == 0x8000;}}}