Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pentamassiv committed Oct 26, 2024
1 parent 83202ea commit 1ead76d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
8 changes: 8 additions & 0 deletions debug_macos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "debug_macos"
version = "0.1.0"
edition = "2021"

[dependencies]
core-graphics = "0.22"
objc = "0.2"
42 changes: 42 additions & 0 deletions debug_macos/src/main.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
7 changes: 3 additions & 4 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ead76d

Please sign in to comment.