Skip to content
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serialize = ["serde"]
unstable_grab = ["evdev-rs", "epoll", "inotify", "dep:serde_json", "serialize"]
wayland = ["input", "input-linux", "xkbcommon"]
x11 = ["dep:x11"]
macos_keyboard_only = []

[target.'cfg(target_os = "macos")'.dependencies]
# cocoa = "0.26"
Expand Down
33 changes: 31 additions & 2 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ use crate::macos::common::*;
use crate::rdev::{Event, ListenError};
use core::ptr::NonNull;
use objc2_core_foundation::{CFMachPort, CFRunLoop, kCFRunLoopCommonModes};
#[cfg(not(feature = "macos_keyboard_only"))]
use objc2_core_graphics::kCGEventMaskForAllEvents;
use objc2_core_graphics::{
CGEvent, CGEventTapCallBack, CGEventTapLocation, CGEventTapOptions, CGEventTapPlacement,
CGEventTapProxy, CGEventType, kCGEventMaskForAllEvents,
CGEventTapProxy, CGEventType,
};
use objc2_foundation::NSAutoreleasePool;
use std::ffi::c_void;
use std::ptr::null_mut;

static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;

#[cfg(feature = "macos_keyboard_only")]
const LISTEN_EVENT_MASK: u64 = (1 << CGEventType::KeyDown.0)
| (1 << CGEventType::KeyUp.0)
| (1 << CGEventType::FlagsChanged.0);

#[cfg(not(feature = "macos_keyboard_only"))]
const LISTEN_EVENT_MASK: u64 = kCGEventMaskForAllEvents as u64;

#[link(name = "Cocoa", kind = "framework")]
unsafe extern "C" {}

Expand Down Expand Up @@ -50,7 +60,7 @@ where
CGEventTapLocation::HIDEventTap, // HID, Session, AnnotatedSession,
CGEventTapPlacement::HeadInsertEventTap,
CGEventTapOptions::ListenOnly,
kCGEventMaskForAllEvents.into(),
LISTEN_EVENT_MASK,
callback,
null_mut(),
)
Expand All @@ -66,3 +76,22 @@ where
}
Ok(())
}

#[cfg(all(test, feature = "macos_keyboard_only"))]
mod tests {
use super::LISTEN_EVENT_MASK;
use objc2_core_graphics::CGEventType;

fn contains(event_type: CGEventType) -> bool {
LISTEN_EVENT_MASK & (1 << event_type.0) != 0
}

#[test]
fn keyboard_only_mask_excludes_mouse_events() {
assert!(contains(CGEventType::KeyDown));
assert!(contains(CGEventType::KeyUp));
assert!(contains(CGEventType::FlagsChanged));
assert!(!contains(CGEventType::MouseMoved));
assert!(!contains(CGEventType::LeftMouseDown));
}
}