From 4c14dee811103d28fad90004693b3e21d118cb7c Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sun, 29 Dec 2024 13:12:09 +0700 Subject: [PATCH 1/2] add key_code to get key's virtual code on keydown/up events https://developer.apple.com/documentation/appkit/nsevent/1534513-keycode?language=objc --- src/appkit/event/mod.rs | 10 +++++++++- src/foundation/mod.rs | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/appkit/event/mod.rs b/src/appkit/event/mod.rs index f58d9999..173cdade 100644 --- a/src/appkit/event/mod.rs +++ b/src/appkit/event/mod.rs @@ -6,7 +6,7 @@ use objc::runtime::Object; use objc::{class, msg_send, msg_send_id, sel}; use crate::events::EventType; -use crate::foundation::{id, nil, NSInteger, NSPoint, NSString}; +use crate::foundation::{id, nil, NSInteger, NSPoint, NSString, NSUInt16}; mod test; @@ -97,6 +97,14 @@ impl Event { unsafe { msg_send![&*self.0, modifierFlags] } } + /// The virtual code for the key associated with the event + pub fn key_code(&self) -> NSUInt16 { + // @TODO: Check here if key event, invalid otherwise. + // @TODO: Figure out if we can just return &str here, since the Objective-C side + // should... make it work, I think. + unsafe { msg_send![&*self.0, keyCode] } + } + /// The indices of the currently pressed mouse buttons. pub fn pressed_mouse_buttons() -> NSUInteger { unsafe { msg_send![class!(NSEvent), pressedMouseButtons] } diff --git a/src/foundation/mod.rs b/src/foundation/mod.rs index f73c0530..d645b2ff 100644 --- a/src/foundation/mod.rs +++ b/src/foundation/mod.rs @@ -87,4 +87,6 @@ pub type NSInteger = libc::c_long; #[cfg(target_pointer_width = "64")] pub type NSUInteger = libc::c_ulong; +pub type NSUInt16 = libc::c_ushort; + pub type NSPoint = core_graphics::geometry::CGPoint; From 3105b798225e5979670d934368f2feda3893a181 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sun, 29 Dec 2024 13:21:57 +0700 Subject: [PATCH 2/2] fmt to look like a readable table --- src/appkit/event/test.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/appkit/event/test.rs b/src/appkit/event/test.rs index 02921b25..736e4304 100644 --- a/src/appkit/event/test.rs +++ b/src/appkit/event/test.rs @@ -1,21 +1,21 @@ -#[cfg(test)] +#[cfg(test)] #[rustfmt::skip] mod event_test { use crate::appkit::EventModifierBitFlag; #[test] fn test_event_modifier_bit_flag_display() { - assert_eq!("‹⇧" , format!("{}",EventModifierBitFlag::LeftShift)); - assert_eq!("‹⇧›" , format!("{}",EventModifierBitFlag::Shift)); - assert_eq!("⇧›" , format!("{}",EventModifierBitFlag::RightShift)); - assert_eq!("‹⇧‹⌃‹⌥‹⌘" , format!("{}",EventModifierBitFlag::LeftModi)); - assert_eq!("⇧›⌃›⌥›⌘›" , format!("{}",EventModifierBitFlag::RightModi)); - assert_eq!("‹⇧›‹⌃›‹⌥›‹⌘›" , format!("{}",EventModifierBitFlag::LRModi)); - assert_eq!("🌐" , format!("{:#}",EventModifierBitFlag::Function)); // when it's a modifier key - assert_eq!("ƒ" , format!("{}",EventModifierBitFlag::Function)); - assert_eq!("⇭" , format!("{:#}",EventModifierBitFlag::Numpad)); // when it's a modifier key - assert_eq!("🔢" , format!("{}",EventModifierBitFlag::Numpad)); - assert_eq!("ℹ" , format!("{}",EventModifierBitFlag::Help)); - assert_eq!("!🖮" , format!("{}",EventModifierBitFlag::DeviceIndependentFlagsMask)); + assert_eq!("‹⇧" ,format!("{}" ,EventModifierBitFlag::LeftShift )); + assert_eq!("‹⇧›" ,format!("{}" ,EventModifierBitFlag::Shift )); + assert_eq!("⇧›" ,format!("{}" ,EventModifierBitFlag::RightShift )); + assert_eq!("‹⇧‹⌃‹⌥‹⌘" ,format!("{}" ,EventModifierBitFlag::LeftModi )); + assert_eq!("⇧›⌃›⌥›⌘›" ,format!("{}" ,EventModifierBitFlag::RightModi )); + assert_eq!("‹⇧›‹⌃›‹⌥›‹⌘›",format!("{}" ,EventModifierBitFlag::LRModi )); + assert_eq!("🌐" ,format!("{:#}",EventModifierBitFlag::Function )); // when it's a modifier key + assert_eq!("ƒ" ,format!("{}" ,EventModifierBitFlag::Function )); + assert_eq!("⇭" ,format!("{:#}",EventModifierBitFlag::Numpad )); // when it's a modifier key + assert_eq!("🔢" ,format!("{}" ,EventModifierBitFlag::Numpad )); + assert_eq!("ℹ" ,format!("{}" ,EventModifierBitFlag::Help )); + assert_eq!("!🖮" ,format!("{}" ,EventModifierBitFlag::DeviceIndependentFlagsMask)); // Shift ignored, only flagmask remains - assert_eq!("!🖮" , format!("{}",EventModifierBitFlag::DeviceIndependentFlagsMask | EventModifierBitFlag::RightShift)); + assert_eq!("!🖮" ,format!("{}" ,EventModifierBitFlag::DeviceIndependentFlagsMask | EventModifierBitFlag::RightShift)); } }