1
1
use std:: { error, fmt} ;
2
2
3
3
use core_graphics:: {
4
- display:: { CGDisplayMoveCursorToPoint , CGError , CGMainDisplayID } ,
5
4
event:: { CGEvent , CGEventTapLocation , CGEventType , CGMouseButton , ScrollEventUnit } ,
6
5
event_source:: { CGEventSource , CGEventSourceStateID } ,
7
6
geometry:: CGPoint ,
@@ -26,7 +25,6 @@ impl Into<CGPoint> for Point {
26
25
27
26
#[ derive( Debug ) ]
28
27
pub enum Error < ' a > {
29
- CGDisplayMoveCursorToPoint ( CGError ) ,
30
28
CGEventNotCreated ,
31
29
CGEventSourceStateInvalid ,
32
30
@@ -38,9 +36,6 @@ impl<'a> error::Error for Error<'a> {}
38
36
impl < ' a > fmt:: Display for Error < ' a > {
39
37
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
40
38
match self {
41
- Error :: CGDisplayMoveCursorToPoint ( err) => {
42
- write ! ( f, "error in call to CGDisplayMoveCursorToPoint: {}" , err)
43
- }
44
39
Error :: CGEventNotCreated => write ! ( f, "CGEvent could not be created" ) ,
45
40
Error :: CGEventSourceStateInvalid => write ! ( f, "invalid CGEventSourceStateID" ) ,
46
41
@@ -66,10 +61,16 @@ impl Mouse {
66
61
pub fn move_to ( & self , x : i32 , y : i32 ) -> Result < ( ) , Box < dyn error:: Error > > {
67
62
let point = CGPoint :: new ( x as _ , y as _ ) ;
68
63
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 ( ( ) )
73
74
}
74
75
75
76
pub fn press < ' a > ( & self , button : & ' a Keys ) -> Result < ( ) , Box < dyn error:: Error + ' a > > {
0 commit comments