From 4c14dee811103d28fad90004693b3e21d118cb7c Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sun, 29 Dec 2024 13:12:09 +0700 Subject: [PATCH] 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;