GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(infra): progress on detecting key state of modifier keys
Lars Berger1f56cdfa3d30
diff --git a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs b/LarsWM.Infrastructure/WindowsApi/KeybindingService.csindex 4e636180..a0f5dc12 100644--- a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs+++ b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs@@ -12,10 +12,10 @@ namespace LarsWM.Infrastructure.WindowsApi{internal class Keybinding{- public Stack<Keys> KeyCombination { get; }+ public List<Keys> KeyCombination { get; }public Action KeybindingProc { get; }- public Keybinding(Stack<Keys> keyCombination, Action keybindingProc)+ public Keybinding(List<Keys> keyCombination, Action keybindingProc){KeyCombination = keyCombination;KeybindingProc = keybindingProc;@@ -24,10 +24,13 @@ namespace LarsWM.Infrastructure.WindowsApipublic class KeybindingService{- public static readonly uint WM_KEYDOWN = 0x100;- public static readonly uint WM_SYSKEYDOWN = 0x104;+ private static readonly uint WM_KEYDOWN = 0x100;+ private static readonly uint WM_SYSKEYDOWN = 0x104;+ private static readonly int WM_KEYUP = 0x0101;+ private static readonly int WM_SYSKEYUP = 0x0105;private List<Keybinding> _registeredKeybindings = new List<Keybinding>();private Keys _modKey = Keys.LMenu;+ private List<Keys> _triggerKeys = new List<Keys>();private Bus _bus;@@ -50,23 +53,20 @@ namespace LarsWM.Infrastructure.WindowsApipublic void AddGlobalKeybinding(string bindingString, Action callback){- if (!bindingString.Contains("$mod"))- throw new Exception($"Key combination '{bindingString}' must include $mod.");-bindingString = bindingString.Replace("$mod", Enum.GetName(typeof(Keys), _modKey));var bindingParts = bindingString.Split('+').Select(key => Enum.Parse(typeof(Keys), key))- .Cast<Keys>();+ .Cast<Keys>()+ .ToList();- var stack = new Stack<Keys>(bindingParts);- var triggerKey = stack.Pop();+ var triggerKey = bindingParts[0];+ bindingParts.RemoveAt(0);- if (triggerKey != _modKey)- throw new Exception($"Key combination '{bindingString}' must use $mod as trigger key.");+ _triggerKeys.Add(triggerKey);- var keybinding = new Keybinding(stack, callback);+ var keybinding = new Keybinding(bindingParts, callback);_registeredKeybindings.Add(keybinding);}@@ -80,27 +80,94 @@ namespace LarsWM.Infrastructure.WindowsApiprivate IntPtr KeybindingHookProc(int nCode, IntPtr wParam, IntPtr lParam){- var passThrough = nCode != 0;+ var passThrough = nCode != 0 || !((uint)wParam == WM_KEYDOWN || (uint)wParam == WM_SYSKEYDOWN);// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.// CallNextHookEx passes the hook notification to other applications.if (passThrough)return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);+ // TODO: Check whether any of the trigger keys were pressed.var modifiersPressed = new List<Keys>();if ((GetKeyState(Keys.LMenu) & 0x8000) == 0x8000)modifiersPressed.Add(Keys.LMenu);+ // if ((GetKeyState(Keys.LShiftKey) & 0x8000) == 0x8000)+ // modifiersPressed.Add(Keys.LShiftKey);++ // if ((GetKeyState(Keys.LControlKey) & 0x8000) == 0x8000)+ // modifiersPressed.Add(Keys.LControlKey);+if (modifiersPressed.Count() == 0)return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);- // Get struct with details about keyboard input event.- var rawEventStruct = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));- var inputEvent = (LowLevelKeyboardInputEvent)rawEventStruct;+ var state = GetCurrentKeyboardState();++ // var isModKeyPressed = IsDown(state, _modKey);++ // if (!isModKeyPressed)+ // return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);++ var keysPressed = new List<Keys>();+ foreach (var key in Enum.GetValues(typeof(Keys)))+ {+ if (IsDown(state, (Keys)key))+ {+ keysPressed.Add((Keys)key);+ Debug.WriteLine("Key is pressed: " + key);+ }+ }++ if (IsDown(state, Keys.LMenu & Keys.J))+ Debug.WriteLine("Key is pressed: Keys.LMenu | Keys.J");++ foreach (var keybinding in _registeredKeybindings)+ {+ var isMatch = keybinding.KeyCombination.All(key => IsDown(state, key));++ if (isMatch)+ keybinding.KeybindingProc();+ }// 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.+ /// </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)+ {+ var virtualKeyCode = (int)key;++ // if (virtualKeyCode < 0 || virtualKeyCode > 255)+ // throw new ArgumentOutOfRangeException("key", key, "The value must be between 0 and 255.");++ var keyState = state.ElementAtOrDefault(virtualKeyCode);++ // if (keyState == null)+ // return false;++ // Get the high-order bit. If the high-order bit is 1, the key is down.+ return keyState >> 7 == 1;}}}