Skip to content

Commit

Permalink
add key_code to get key's virtual code on keydown/up events
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenesvk committed Nov 14, 2024
1 parent 6fc579f commit 5d71561
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/appkit/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/// An EventMask describes the type of event.
#[bitmask(u64)]
Expand Down Expand Up @@ -83,6 +83,14 @@ impl Event {
characters.to_string()
}

/// 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] }
Expand Down
2 changes: 2 additions & 0 deletions src/foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 5d71561

Please sign in to comment.