Skip to content

Commit ab35f1e

Browse files
authored
Merge pull request #21 from bwpge/main
Fix move_to event issue on macOS (#19)
2 parents 7d2ca5d + 2eca918 commit ab35f1e

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/sys/macos.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{error, fmt};
22

33
use core_graphics::{
4-
display::{CGDisplayMoveCursorToPoint, CGError, CGMainDisplayID},
54
event::{CGEvent, CGEventTapLocation, CGEventType, CGMouseButton, ScrollEventUnit},
65
event_source::{CGEventSource, CGEventSourceStateID},
76
geometry::CGPoint,
@@ -26,7 +25,6 @@ impl Into<CGPoint> for Point {
2625

2726
#[derive(Debug)]
2827
pub enum Error<'a> {
29-
CGDisplayMoveCursorToPoint(CGError),
3028
CGEventNotCreated,
3129
CGEventSourceStateInvalid,
3230

@@ -38,9 +36,6 @@ impl<'a> error::Error for Error<'a> {}
3836
impl<'a> fmt::Display for Error<'a> {
3937
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4038
match self {
41-
Error::CGDisplayMoveCursorToPoint(err) => {
42-
write!(f, "error in call to CGDisplayMoveCursorToPoint: {}", err)
43-
}
4439
Error::CGEventNotCreated => write!(f, "CGEvent could not be created"),
4540
Error::CGEventSourceStateInvalid => write!(f, "invalid CGEventSourceStateID"),
4641

@@ -66,10 +61,16 @@ impl Mouse {
6661
pub fn move_to(&self, x: i32, y: i32) -> Result<(), Box<dyn error::Error>> {
6762
let point = CGPoint::new(x as _, y as _);
6863

69-
match unsafe { CGDisplayMoveCursorToPoint(CGMainDisplayID(), point) } {
70-
0 => Ok(()),
71-
err => Err(Box::new(Error::CGDisplayMoveCursorToPoint(err))),
72-
}
64+
CGEvent::new_mouse_event(
65+
Self::event_source()?,
66+
CGEventType::MouseMoved,
67+
point,
68+
CGMouseButton::Left, // ignored
69+
)
70+
.or(Err(Error::CGEventNotCreated))?
71+
.post(CGEventTapLocation::HID);
72+
73+
Ok(())
7374
}
7475

7576
pub fn press<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn error::Error + 'a>> {

0 commit comments

Comments
 (0)