diff options
author | Teddy Wing | 2016-11-16 04:12:56 -0500 |
---|---|---|
committer | Teddy Wing | 2016-11-16 04:12:56 -0500 |
commit | 1832514ab872dec621d5f403eb5328167b3da033 (patch) | |
tree | a863ea28f19cddec1ffdd49db4c837c003db92bf | |
parent | a3c91c19679d43e0298fe89c4654bb0645c7c0a2 (diff) | |
download | Low-Battery-Yup-1832514ab872dec621d5f403eb5328167b3da033.tar.bz2 |
Mouse: Implement `click`
Method that clicks the mouse.
* Add `_cursor_position` instance var that describes the current cursor
position after the `moveToPoint` method is called
* `click` will click at the `_cursor_position` point
* I had extracted the centering code to `centerPoint` (which maybe
should have been called `pointAtCenter`) out of the `moveToCenter`
method because I briefly used it in `click` as a test before adding
the `_cursor_position` ivar. This should be reintegrated with
`moveToCenter`.
* Add a `post_mouse_event` function that abstracts the CG calls needed
to send a mouse event since we need both click down and up events.
Based on:
http://stackoverflow.com/questions/1483657/performing-a-double-click-using-cgeventcreatemouseevent
-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 |