diff options
-rw-r--r-- | Low Battery Yup d/AppDelegate.m | 1 | ||||
-rw-r--r-- | Low Battery Yup d/Mouse.h | 2 | ||||
-rw-r--r-- | Low Battery Yup d/Mouse.m | 30 |
3 files changed, 27 insertions, 6 deletions
diff --git a/Low Battery Yup d/AppDelegate.m b/Low Battery Yup d/AppDelegate.m index d599f8a..887d794 100644 --- a/Low Battery Yup d/AppDelegate.m +++ b/Low Battery Yup d/AppDelegate.m @@ -20,6 +20,7 @@ { Mouse *m = [[Mouse alloc] init]; [m moveToCenter]; + [m click]; } @end diff --git a/Low Battery Yup d/Mouse.h b/Low Battery Yup d/Mouse.h index 950f9c1..d34e715 100644 --- a/Low Battery Yup d/Mouse.h +++ b/Low Battery Yup d/Mouse.h @@ -10,8 +10,10 @@ @interface Mouse : NSObject { CGDirectDisplayID _current_display; + CGPoint _cursor_position; } +- (CGPoint)centerPoint; - (void)moveToPoint:(CGPoint)point; - (void)moveToCenter; - (void)click; diff --git a/Low Battery Yup d/Mouse.m b/Low Battery Yup d/Mouse.m index ead9010..91ddce4 100644 --- a/Low Battery Yup d/Mouse.m +++ b/Low Battery Yup d/Mouse.m @@ -15,16 +15,12 @@ self = [super init]; if (self) { _current_display = CGMainDisplayID(); + _cursor_position = CGPointMake(0, 0); } return self; } -- (void)moveToPoint:(CGPoint)point -{ - CGDisplayMoveCursorToPoint(_current_display, point); -} - -- (void)moveToCenter +- (CGPoint)centerPoint { CGPoint point; @@ -34,11 +30,33 @@ point.x = width / 2; point.y = height / 2; + return point; +} + +- (void)moveToPoint:(CGPoint)point +{ + _cursor_position = point; + CGDisplayMoveCursorToPoint(_current_display, point); +} + +- (void)moveToCenter +{ + CGPoint point = [self centerPoint]; [self moveToPoint:point]; } +void post_mouse_event (CGEventType type, CGMouseButton button, CGPoint point) { + CGEventSourceRef ref = CGEventSourceCreate(kCGEventSourceStatePrivate); + CGEventRef event = CGEventCreateMouseEvent(ref, type, point, button); + CGEventPost(kCGHIDEventTap, event); + CFRelease(event); + CFRelease(ref); +} + - (void)click { + post_mouse_event(kCGEventLeftMouseDown, kCGMouseButtonLeft, _cursor_position); + post_mouse_event(kCGEventLeftMouseUp, kCGMouseButtonLeft, _cursor_position); } @end |