11use std:: { error, fmt} ;
22
33use 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 ) ]
2827pub enum Error < ' a > {
29- CGDisplayMoveCursorToPoint ( CGError ) ,
3028 CGEventNotCreated ,
3129 CGEventSourceStateInvalid ,
3230
@@ -38,9 +36,6 @@ impl<'a> error::Error for Error<'a> {}
3836impl < ' 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