Permalink
Browse files

Change CGPostMouseEvent to CGEventCreateMouseEvent

  • Loading branch information...
1 parent 68ac877 commit dba971d5298a97a40c2e048e6504c48bf35b7045 @taromati taromati committed Jul 12, 2016
Showing with 23 additions and 15 deletions.
  1. +23 −15 autoClicker.m
View
38 autoClicker.m 100644 → 100755
@@ -1,20 +1,28 @@
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
-int main(int argc, char *argv[]) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
-
- int x = [args integerForKey:@"x"];
- int y = [args integerForKey:@"y"];
-
- CGPoint pt;
- pt.x = x;
- pt.y = y;
+void postMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point) {
+ CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
+ CGEventSetType(theEvent, type);
+ CGEventPost(kCGHIDEventTap, theEvent);
+ CFRelease(theEvent);
+}
- CGPostMouseEvent( pt, 1, 1, 1 );
- CGPostMouseEvent( pt, 1, 1, 0 );
-
- [pool release];
+int main(int argc, char *argv[]) {
+ @autoreleasepool {
+ NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
+
+ int x = [args integerForKey:@"x"];
+ int y = [args integerForKey:@"y"];
+
+ CGPoint pt;
+ pt.x = x;
+ pt.y = y;
+
+ postMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, CGPointMake(x, y));
+ usleep(100 * 1000);
+ postMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, CGPointMake(x, y));
+ }
+
return 0;
-}
+}

0 comments on commit dba971d

Please sign in to comment.