Skip to content

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

feat: add partially implemented check for whether there's a matching keybinding

Lars Berger1543afcfc9f9
diff --git a/packages/wm/src/common/platform/keyboard_hook.rs b/packages/wm/src/common/platform/keyboard_hook.rs
index 881373e5..0067892d 100644
--- a/packages/wm/src/common/platform/keyboard_hook.rs
+++ b/packages/wm/src/common/platform/keyboard_hook.rs
@@ -18,9 +18,22 @@ use crate::user_config::KeybindingConfig;
use super::PlatformEvent;
thread_local! {
+ /// Thread-local for instance of `KeyboardHook`.
+ ///
+ /// For use with hook procedure.
static KEYBOARD_HOOK: OnceCell<KeyboardHook> = OnceCell::new();
}
+/// Available modifier keys.
+const MODIFIER_KEYS: [u16; 6] = [
+ VK_LSHIFT.0,
+ VK_RSHIFT.0,
+ VK_LCONTROL.0,
+ VK_RCONTROL.0,
+ VK_LMENU.0,
+ VK_RMENU.0,
+];
+
pub fn set_local_keyboard_hook(hook: KeyboardHook) {
let _ = KEYBOARD_HOOK.with(|cell| cell.set(hook));
}
@@ -299,13 +312,104 @@ impl KeyboardHook {
match self.keybindings_by_trigger_key.get(&vk_code) {
// Forward the event if no keybindings exist for the trigger key.
None => false,
- Some(keybinding) => {
- // TODO: Emit platform event.
- // let _ = self.event_tx.send(keybinding.clone());
+ // Otherwise, check if there is a matching keybinding.
+ Some(keybindings) => {
+ // let mut cached_key_states = HashMap::new();
+
+ // // Find the matching keybindings based on the pressed keys.
+ // let matched_keybindings =
+ // keybindings.iter().filter(|keybinding| {
+ // keybinding.vk_codes.iter().all(|&key| {
+ // if key == vk_code {
+ // return true;
+ // }
+
+ // if let Some(&is_key_down) = cached_key_states.get(&key) {
+ // return is_key_down;
+ // }
+
+ // let is_key_down = Self::is_key_down(key);
+ // cached_key_states.insert(key, is_key_down);
+ // is_key_down
+ // })
+ // });
+
+ // // Find the longest matching keybinding.
+ // let longest_keybinding = matched_keybindings
+ // .max_by_key(|keybinding| keybinding.vk_codes.len());
+
+ // if longest_keybinding.is_none() {
+ // return false;
+ // }
+
+ // let longest_keybinding = longest_keybinding.unwrap();
+
+ // // Get the modifier keys to reject based on the longest matching keybinding.
+ // let modifier_keys_to_reject =
+ // MODIFIER_KEYS.iter().filter(|&&modifier_key| {
+ // !longest_keybinding.vk_codes.contains(&modifier_key)
+ // && !longest_keybinding
+ // .vk_codes
+ // .contains(&Self::get_generic_key(modifier_key))
+ // });
+
+ // // Check if any modifier keys to reject are currently down.
+ // let has_modifier_keys_to_reject =
+ // modifier_keys_to_reject.any(|&modifier_key| {
+ // if let Some(&is_key_down) =
+ // cached_key_states.get(&modifier_key)
+ // {
+ // is_key_down
+ // } else {
+ // Self::is_key_down(modifier_key)
+ // }
+ // });
+
+ // if has_modifier_keys_to_reject {
+ // return false;
+ // }
+
+ // // Invoke the callback function for the longest matching keybinding.
+ // let _ = self.event_tx.send(PlatformEvent::KeybindingTriggered(
+ // longest_keybinding.clone(),
+ // ));
true
}
}
}
+
+ // /// Gets the generic key code for a given key code.
+ // fn get_generic_key(key: u16) -> u16 {
+ // match key {
+ // VK_LMENU | VK_RMENU => VK_MENU,
+ // VK_LSHIFT | VK_RSHIFT => VK_SHIFT,
+ // VK_LCONTROL | VK_RCONTROL => VK_CONTROL,
+ // _ => key,
+ // }
+ // }
+
+ // /// Checks if the specified key is currently down.
+ // fn is_key_down(key: u16) -> bool {
+ // match key {
+ // VK_MENU => {
+ // Self::is_key_down_raw(VK_LMENU) || Self::is_key_down_raw(VK_RMENU)
+ // }
+ // VK_SHIFT => {
+ // Self::is_key_down_raw(VK_LSHIFT)
+ // || Self::is_key_down_raw(VK_RSHIFT)
+ // }
+ // VK_CONTROL => {
+ // Self::is_key_down_raw(VK_LCONTROL)
+ // || Self::is_key_down_raw(VK_RCONTROL)
+ // }
+ // _ => Self::is_key_down_raw(key),
+ // }
+ // }
+
+ // /// Checks if the specified key is currently down using the raw key code.
+ // fn is_key_down_raw(key: u16) -> bool {
+ // unsafe { (GetKeyState(key) & 0x8000) == 0x8000 }
+ // }
}
extern "system" fn keyboard_hook_proc(