From 1ead76de372dc9bb58480a54fe2c658dd1a956e6 Mon Sep 17 00:00:00 2001 From: pentamassiv Date: Fri, 25 Oct 2024 22:33:16 +0200 Subject: [PATCH] WIP --- debug_macos/Cargo.toml | 8 ++++++++ debug_macos/src/main.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/macos/macos_impl.rs | 7 +++---- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 debug_macos/Cargo.toml create mode 100644 debug_macos/src/main.rs diff --git a/debug_macos/Cargo.toml b/debug_macos/Cargo.toml new file mode 100644 index 00000000..37183827 --- /dev/null +++ b/debug_macos/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "debug_macos" +version = "0.1.0" +edition = "2021" + +[dependencies] +core-graphics = "0.22" +objc = "0.2" diff --git a/debug_macos/src/main.rs b/debug_macos/src/main.rs new file mode 100644 index 00000000..5d5e7310 --- /dev/null +++ b/debug_macos/src/main.rs @@ -0,0 +1,42 @@ +use core_graphics::event::{ + CGEvent, CGEventFlags, CGEventTap, CGEventTapLocation, CGEventType, EventField, +}; +use core_graphics::sys::CGEventMask; +use std::ptr; + +fn main() { + // Set up an event tap for key down and key up events + let event_mask = CGEventMask(1 << CGEventType::KeyDown as u64 | 1 << CGEventType::KeyUp as u64); + + unsafe { + let tap = CGEventTap::new( + CGEventTapLocation::HID, + CGEventType::KeyDown, + event_mask, + move |event| { + // Access the event's flags + let flags = event.get_flags(); + + // Print the flags (e.g., control, shift, command, etc.) + println!("Key Event with flags: {:?}", flags); + + // You can get specific keys like so: + if flags.contains(CGEventFlags::CGEventFlagMaskShift) { + println!("Shift key is pressed"); + } + if flags.contains(CGEventFlags::CGEventFlagMaskControl) { + println!("Control key is pressed"); + } + if flags.contains(CGEventFlags::CGEventFlagMaskCommand) { + println!("Command key is pressed"); + } + + // Pass the event through to the next receiver + event + }, + ) + .expect("Failed to create event tap"); + + tap.start().expect("Failed to start event tap"); + } +} diff --git a/src/macos/macos_impl.rs b/src/macos/macos_impl.rs index 3afe87c2..559ec864 100644 --- a/src/macos/macos_impl.rs +++ b/src/macos/macos_impl.rs @@ -444,9 +444,9 @@ impl Keyboard for Enigo { EventField::EVENT_SOURCE_USER_DATA, self.event_source_user_data, ); - self.add_event_flag(keycode, Direction::Press); event.set_flags(self.event_flags); event.post(CGEventTapLocation::HID); + self.add_event_flag(keycode, Direction::Press); } if direction == Direction::Click || direction == Direction::Release { @@ -461,9 +461,9 @@ impl Keyboard for Enigo { EventField::EVENT_SOURCE_USER_DATA, self.event_source_user_data, ); - self.add_event_flag(keycode, Direction::Release); event.set_flags(self.event_flags); event.post(CGEventTapLocation::HID); + self.add_event_flag(keycode, Direction::Release); } match direction { @@ -661,8 +661,7 @@ impl Enigo { KeyCode::OPTION | KeyCode::RIGHT_OPTION => CGEventFlags::CGEventFlagAlternate, KeyCode::COMMAND | KeyCode::RIGHT_COMMAND => CGEventFlags::CGEventFlagCommand, KeyCode::HELP => CGEventFlags::CGEventFlagHelp, - // KeyCode:: => CGEventFlags::CGEventFlagSecondaryFn, TODO: Check if a KeyCode exists - // that requires this flag to get set + KeyCode::FUNCTION => CGEventFlags::CGEventFlagSecondaryFn, 83 => CGEventFlags::CGEventFlagNumericPad, // Numpad 1 84 => CGEventFlags::CGEventFlagNumericPad, // Numpad 2 85 => CGEventFlags::CGEventFlagNumericPad, // Numpad 3