Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(infra): use `MouseKeyHook` to register combination keybindings

Lars Bergere8f2734b83c5
diff --git a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs
index a0f5dc12..d94fcbf1 100644
--- a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs
+++ b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs
@@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
-using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
+using Gma.System.MouseKeyHook;
using LarsWM.Infrastructure.Bussing;
using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
@@ -26,13 +26,12 @@ namespace LarsWM.Infrastructure.WindowsApi
{
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 Keys _modKey = Keys.Alt;
private List<Keys> _triggerKeys = new List<Keys>();
private Bus _bus;
+ private IKeyboardMouseEvents m_GlobalHook;
public KeybindingService(Bus bus)
{
@@ -51,28 +50,39 @@ namespace LarsWM.Infrastructure.WindowsApi
_modKey = (Keys)Enum.Parse(typeof(Keys), modKey);
}
- public void AddGlobalKeybinding(string bindingString, Action callback)
+ public void AddGlobalKeybinding(string binding, Action callback)
{
- bindingString = bindingString.Replace("$mod", Enum.GetName(typeof(Keys), _modKey));
-
- var bindingParts = bindingString
+ var bindingParts = binding
.Split('+')
- .Select(key => Enum.Parse(typeof(Keys), key))
- .Cast<Keys>()
- .ToList();
+ .Select(key => FormatKeybinding(key));
+
+ var formattedBinding = string.Join("+", bindingParts);
+ var combinationBinding = Combination.FromString(formattedBinding);
+
+ var combinationActionDic = new Dictionary<Combination, Action>
+ {
+ { combinationBinding, callback }
+ };
+
+ m_GlobalHook.OnCombination(combinationActionDic);
+ }
+
+ private string FormatKeybinding(string key)
+ {
+ if (key == "$mod")
+ return Enum.GetName(typeof(Keys), _modKey);
- var triggerKey = bindingParts[0];
- bindingParts.RemoveAt(0);
+ var isNumeric = int.TryParse(key, out int _);
- _triggerKeys.Add(triggerKey);
+ if (isNumeric)
+ return $"D{key}";
- var keybinding = new Keybinding(bindingParts, callback);
- _registeredKeybindings.Add(keybinding);
+ return key;
}
private void CreateKeybindingHook()
{
- SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KeybindingHookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
+ m_GlobalHook = Hook.GlobalEvents();
// `SetWindowsHookEx` requires a message loop within the thread that is executing the code.
Application.Run();