Skip to content

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

feat: register keyboard hook procedure in KeybindingService.Init

Larsd418354c5b8c
diff --git a/LarsWM.Core/Common/Services/KeybindingService.cs b/LarsWM.Core/Common/Services/KeybindingService.cs
index 7c62646d..1f33e875 100644
--- a/LarsWM.Core/Common/Services/KeybindingService.cs
+++ b/LarsWM.Core/Common/Services/KeybindingService.cs
@@ -3,50 +3,54 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
+using System.Reactive;
using System.Threading;
using System.Windows.Forms;
+using System.Runtime.InteropServices;
+using System.Reactive.Subjects;
namespace LarsWM.Core.Common.Services
{
class KeybindingService
{
+ private Subject<string> _modKeypresses = new Subject<string>();
+
public void Init()
{
-
// SetWindowsHookEx requires a message loop within the thread that is executing the code.
var thread = new Thread(() =>
{
- SetWindowsHookEx(WH_KEYBOARD_LL, _kbdHook, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
- SetWindowsHookEx(WH_MOUSE_LL, _mouseHook, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
+ SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KbHookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
+ //SetWindowsHookEx(HookType.WH_MOUSE_LL, _mouseHook, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
Application.Run();
});
thread.Name = "LarsWMKeybindingService";
thread.Start();
+
+ HandleKeybindings();
}
- private int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
+ private IntPtr KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
- if (nCode >= 0) // This means we can intercept the event.
- {
- var hookStruct = (KbLLHookStruct)Marshal.PtrToStructure(
- lParam,
- typeof(KbLLHookStruct));
-
- // Quick check if Ctrl key is down.
- // See GetKeyState() doco for more info about the flags.
- bool ctrlDown =
- GetKeyState(VK_LCONTROL) != 0 ||
- GetKeyState(VK_RCONTROL) != 0;
-
- if (ctrlDown && hookStruct.vkCode == 0x56) // Ctrl+V
- {
- // Replace this with your custom action.
- Clipboard.SetText("Hi");
- }
- }
-
- // Pass to other keyboard handlers. Makes the Ctrl+V pass through.
- return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
+ // If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
+ if (nCode < 0)
+ return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
+
+ // Get structure with details about keyboard input event.
+ var hookStruct = (KbLLHookStruct) Marshal.PtrToStructure(lParam, typeof(KbLLHookStruct));
+
+ var key = hookStruct.vkCode;
+
+ if (key == Keys.LWin)
+ _modKeypresses.OnNext("");
+
+ // Pass hook notification to other applications.
+ return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
+ }
+
+ private void HandleKeybindings()
+ {
+ _modKeypresses.Subscribe();
}
}
}