GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: removed GetDownKeys method in favor of extracting key from lParam
Lars1d7e440c1464
diff --git a/LarsWM.Core/Common/Services/KeybindingService.cs b/LarsWM.Core/Common/Services/KeybindingService.csindex 5b71bd61..2c4b57ac 100644--- a/LarsWM.Core/Common/Services/KeybindingService.cs+++ b/LarsWM.Core/Common/Services/KeybindingService.cs@@ -1,22 +1,28 @@-using static LarsWM.Core.WindowsApi.WindowsApiService;+using LarsWM.Core.Monitors.Events;using System;using System.Collections.Generic;using System.Diagnostics;-using System.Text;-using System.Reactive;+using System.Linq;+using System.Reactive.Subjects;+using System.Runtime.InteropServices;using System.Threading;using System.Windows.Forms;-using System.Runtime.InteropServices;-using System.Reactive.Subjects;-using System.Windows.Input;-using System.Linq;-using System.ComponentModel;+using static LarsWM.Core.WindowsApi.WindowsApiService;namespace LarsWM.Core.Common.Services{class KeybindingService{- private Subject<List<Key>> _modKeypresses = new Subject<List<Key>>();+ private Subject<Keys> _modKeypresses = new Subject<Keys>();+ public static readonly uint WM_KEYDOWN = 0x100;+ public static readonly uint WM_SYSKEYDOWN = 0x104;++ private IBus _bus;++ public KeybindingService(IBus bus)+ {+ _bus = bus;+ }public void Init(){@@ -33,52 +39,27 @@ namespace LarsWM.Core.Common.ServicesHandleKeybindings();}- public static readonly uint WM_KEYDOWN = 0x100;- public static readonly uint WM_SYSKEYDOWN = 0x104;-- [Flags]- public enum KeyModifiers- {- None = 0,-- LControl = 1,- RControl = 2,- Control = LControl | RControl,-- LShift = 4,- RShift = 8,- Shift = LShift | RShift,-- LAlt = 16,- RAlt = 32,- Alt = LAlt | RAlt,-- LWin = 64,- RWin = 128,- Win = LWin | RWin- }private IntPtr KbHookProc(int nCode, IntPtr wParam, IntPtr lParam){// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.// CallNextHookEx passes hook notification to other applications.+ // TODO: Flatten this if-statement.if (nCode == 0 && ((uint)wParam == WM_KEYDOWN || (uint)wParam == WM_SYSKEYDOWN)){- KeyModifiers modifiersPressed = 0;+ var modifiersPressed = new List<Keys>();++ if ((GetKeyState(Keys.LMenu) & 0x8000) == 0x8000)+ modifiersPressed.Add(Keys.LMenu);- if ((GetKeyState((Key)0xA4) & 0x8000) == 0x8000)- modifiersPressed |= KeyModifiers.LAlt;+ if (modifiersPressed.Count() == 0)+ return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);- if (modifiersPressed != KeyModifiers.None)- {- var downKeys = GetDownKeys().ToList();- foreach (var key in downKeys)- {- Debug.WriteLine(key);- }- _modKeypresses.OnNext(downKeys);- return new IntPtr(1);- }+ // Get structure with details about keyboard input event.+ var hookStruct = (KbLLHookStruct) Marshal.PtrToStructure(lParam, typeof(KbLLHookStruct));++ _modKeypresses.OnNext(hookStruct.vkCode);+ return new IntPtr(1);}return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);@@ -86,48 +67,12 @@ namespace LarsWM.Core.Common.Servicesprivate void HandleKeybindings(){- _modKeypresses.Subscribe(downKeys =>+ _modKeypresses.Subscribe(pressedKey =>{- Debug.WriteLine("is called");- //foreach (var key in downKeys)- //{- // Debug.WriteLine(key);- //}+ if (pressedKey == Keys.J)+ _bus.RaiseEvent(new MonitorAddedEvent());});}-- private static readonly byte[] DistinctVirtualKeys = Enumerable- .Range(0, 256)- .Select(KeyInterop.KeyFromVirtualKey)- .Where(item => item != Key.None)- .Distinct()- .Select(item => (byte)KeyInterop.VirtualKeyFromKey(item))- .ToArray();-- /// <summary>- /// Gets all keys that are currently in the down state.- /// </summary>- public IEnumerable<Key> GetDownKeys()- {- var keyboardState = new byte[256];- GetKeyboardState(keyboardState);-- var downKeys = new List<Key>();- for (var index = 0; index < DistinctVirtualKeys.Length; index++)- {- var virtualKey = DistinctVirtualKeys[index];- if ((keyboardState[virtualKey] & 0x80) != 0)- {- downKeys.Add(KeyInterop.KeyFromVirtualKey(virtualKey));- }- }-- return downKeys;- }-- [DllImport("user32.dll")]- [return: MarshalAs(UnmanagedType.Bool)]- private static extern bool GetKeyboardState(byte[] keyState);}}diff --git a/LarsWM.Core/Common/Services/WindowsApiService.cs b/LarsWM.Core/Common/Services/WindowsApiService.csindex 2df8c8a2..4fc3750b 100644--- a/LarsWM.Core/Common/Services/WindowsApiService.cs+++ b/LarsWM.Core/Common/Services/WindowsApiService.cs@@ -196,7 +196,7 @@ namespace LarsWM.Core.WindowsApi[StructLayout(LayoutKind.Sequential)]public class KbLLHookStruct{- public Key vkCode;+ public Keys vkCode;public int scanCode;public int flags;public int time;@@ -210,6 +210,6 @@ namespace LarsWM.Core.WindowsApipublic static extern IntPtr CallNextHookEx([Optional] IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);[DllImport("user32.dll")]- public static extern short GetKeyState(Key nVirtKey);+ public static extern short GetKeyState(Keys nVirtKey);}}