GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: working keyboard hook; invoke commands on keybinding
Lars Berger7995d287980d
diff --git a/packages/wm/src/common/platform/keyboard_hook.rs b/packages/wm/src/common/platform/keyboard_hook.rsindex ea0ce943..0333a9b9 100644--- a/packages/wm/src/common/platform/keyboard_hook.rs+++ b/packages/wm/src/common/platform/keyboard_hook.rs@@ -319,102 +319,104 @@ impl KeyboardHook {None => false,// 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(),- // ));+ 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 mut 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.config.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 }- // }+ /// Gets the generic key code for a given key code.+ fn get_generic_key(key: u16) -> u16 {+ match VIRTUAL_KEY(key) {+ VK_LMENU | VK_RMENU => VK_MENU.0,+ VK_LSHIFT | VK_RSHIFT => VK_SHIFT.0,+ VK_LCONTROL | VK_RCONTROL => VK_CONTROL.0,+ _ => key,+ }+ }++ /// Checks if the specified key is currently down.+ fn is_key_down(key: u16) -> bool {+ match VIRTUAL_KEY(key) {+ VK_MENU => {+ Self::is_key_down_raw(VK_LMENU.0)+ || Self::is_key_down_raw(VK_RMENU.0)+ }+ VK_SHIFT => {+ Self::is_key_down_raw(VK_LSHIFT.0)+ || Self::is_key_down_raw(VK_RSHIFT.0)+ }+ VK_CONTROL => {+ Self::is_key_down_raw(VK_LCONTROL.0)+ || Self::is_key_down_raw(VK_RCONTROL.0)+ }+ _ => 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.into()) & 0x80) == 0x80 }+ }}extern "system" fn keyboard_hook_proc(diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rsindex a381dc05..9962d650 100644--- a/packages/wm/src/wm.rs+++ b/packages/wm/src/wm.rs@@ -43,24 +43,31 @@ impl WindowManager {event: PlatformEvent,config: &mut UserConfig,) {- let mut state = self.state.lock().await;-let res = match event {PlatformEvent::DisplaySettingsChanged => Ok(()),- PlatformEvent::KeybindingTriggered(_) => Ok(()),- PlatformEvent::MouseMove => Ok(()),- PlatformEvent::WindowDestroyed(w) => {- handle_window_destroyed(w, state.deref_mut(), config)+ PlatformEvent::KeybindingTriggered(kb_config) => {+ for command in kb_config.commands {+ self.process_command(command, config).await;+ }+ Ok(())}+ PlatformEvent::MouseMove => Ok(()),+ PlatformEvent::WindowDestroyed(window) => handle_window_destroyed(+ window,+ self.state.lock().await.deref_mut(),+ config,+ ),PlatformEvent::WindowFocused(_) => Ok(()),PlatformEvent::WindowHidden(_) => Ok(()),PlatformEvent::WindowLocationChanged(_) => Ok(()),PlatformEvent::WindowMinimized(_) => Ok(()),PlatformEvent::WindowMinimizeEnded(_) => Ok(()),PlatformEvent::WindowMovedOrResized(_) => Ok(()),- PlatformEvent::WindowShown(w) => {- handle_window_shown(w, state.deref_mut(), config)- }+ PlatformEvent::WindowShown(window) => handle_window_shown(+ window,+ self.state.lock().await.deref_mut(),+ config,+ ),PlatformEvent::WindowTitleChanged(_) => Ok(()),};