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

feat: enable mouse move event when mouse is pressed on macos #12

Open
wants to merge 2 commits into
base: master
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
34 changes: 34 additions & 0 deletions examples/dragging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::sync::atomic::AtomicBool;

use rdev::{listen, Event, EventType};

static DRAGGING: AtomicBool = AtomicBool::new(false);

fn callback(event: Event) {
match event.event_type {
EventType::MouseMove { x, y } => {
if DRAGGING.load(std::sync::atomic::Ordering::Relaxed) {
println!("Mouse dragged to ({}, {})", x, y);
} else {
println!("Mouse moved to ({}, {})", x, y);
}
}
EventType::ButtonPress(button) => {
println!("Mouse button pressed: {:?}", button);
DRAGGING.store(true, std::sync::atomic::Ordering::Relaxed);
}
EventType::ButtonRelease(button) => {
println!("Mouse button released: {:?}", button);
DRAGGING.store(false, std::sync::atomic::Ordering::Relaxed);
}
_ => {
println!("Unhandled Event: {:?}", event);
}
}
}

fn main() {
if let Err(error) = listen(callback) {
println!("Error: {:?}", error)
}
}
2 changes: 1 addition & 1 deletion src/codes_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod test {
continue;
}
if let Some(code2) = super::usb_hid_code_to_macos_code(usb_hid) {
assert_eq!(code, code2 as u32)
assert_eq!(code, code2 as u16)
} else {
assert!(false, "We could not convert back code: {:?}", code);
}
Expand Down
14 changes: 14 additions & 0 deletions src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ pub unsafe fn convert(
CGEventType::LeftMouseUp => Some(EventType::ButtonRelease(Button::Left)),
CGEventType::RightMouseDown => Some(EventType::ButtonPress(Button::Right)),
CGEventType::RightMouseUp => Some(EventType::ButtonRelease(Button::Right)),
CGEventType::LeftMouseDragged => {
let point = cg_event.location();
Some(EventType::MouseMove {
x: point.x,
y: point.y,
})
}
CGEventType::RightMouseDragged => {
let point = cg_event.location();
Some(EventType::MouseMove {
x: point.x,
y: point.y,
})
}
CGEventType::MouseMoved => {
let point = cg_event.location();
Some(EventType::MouseMove {
Expand Down