Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supoprt for getting key_code on key events #139

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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};

mod test;

Expand Down Expand Up @@ -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] }
Expand Down
28 changes: 14 additions & 14 deletions src/appkit/event/test.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}
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;
Loading