Clean up root folder.
[enjoyable.git] / Classes / NJOutputMouseButton.m
diff --git a/Classes/NJOutputMouseButton.m b/Classes/NJOutputMouseButton.m
new file mode 100644 (file)
index 0000000..d5b1f16
--- /dev/null
@@ -0,0 +1,51 @@
+//
+//  NJOutputMouseButton.m
+//  Enjoy
+//
+//  Created by Yifeng Huang on 7/27/12.
+//
+
+#import "NJOutputMouseButton.h"
+
+@implementation NJOutputMouseButton
+
++ (NSString *)serializationCode {
+    return @"mouse button";
+}
+
+- (NSDictionary *)serialize {
+    return @{ @"type": self.class.serializationCode, @"button": @(_button) };
+}
+
++ (NJOutput *)outputDeserialize:(NSDictionary *)serialization
+                  withMappings:(NSArray *)mappings {
+    NJOutputMouseButton *output = [[NJOutputMouseButton alloc] init];
+    output.button = [serialization[@"button"] intValue];
+    return output;
+}
+
+- (void)trigger {
+    CGFloat height = NSScreen.mainScreen.frame.size.height;
+    NSPoint mouseLoc = NSEvent.mouseLocation;
+    CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
+    CGEventRef click = CGEventCreateMouseEvent(NULL,
+                                               eventType,
+                                               CGPointMake(mouseLoc.x, height - mouseLoc.y),
+                                               _button);
+    CGEventPost(kCGHIDEventTap, click);
+    CFRelease(click);
+}
+
+- (void)untrigger {
+    CGFloat height = NSScreen.mainScreen.frame.size.height;
+    NSPoint mouseLoc = NSEvent.mouseLocation;
+    CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
+    CGEventRef click = CGEventCreateMouseEvent(NULL,
+                                               eventType,
+                                               CGPointMake(mouseLoc.x, height - mouseLoc.y),
+                                               _button);
+    CGEventPost(kCGHIDEventTap, click);
+    CFRelease(click);
+}
+
+@end