Replace hacky target stringification with structured serialization.
[enjoyable.git] / TargetMouseBtn.m
1 //
2 // TargetMouseBtn.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/27/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TargetMouseBtn.h"
10
11 @implementation TargetMouseBtn
12
13 @synthesize which;
14
15 + (NSString *)serializationCode {
16 return @"mbtn";
17 }
18
19 - (NSDictionary *)serialize {
20 return @{ @"type": @"mbtn", @"which": @(self.which) };
21 }
22
23 + (Target *)targetDeserialize:(NSDictionary *)serialization
24 withConfigs:(NSArray *)configs {
25 TargetMouseBtn *target = [[TargetMouseBtn alloc] init];
26 target.which = [serialization[@"which"] intValue];
27 return target;
28 }
29
30 -(void) trigger {
31 NSRect screenRect = [[NSScreen mainScreen] frame];
32 NSInteger height = screenRect.size.height;
33 NSPoint mouseLoc = [NSEvent mouseLocation];
34 CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
35 CGEventRef click = CGEventCreateMouseEvent(NULL,
36 eventType,
37 CGPointMake(mouseLoc.x, height - mouseLoc.y),
38 which);
39 CGEventPost(kCGHIDEventTap, click);
40 CFRelease(click);
41 }
42
43 -(void) untrigger {
44 NSRect screenRect = [[NSScreen mainScreen] frame];
45 NSInteger height = screenRect.size.height;
46 NSPoint mouseLoc = [NSEvent mouseLocation];
47 CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
48 CGEventRef click = CGEventCreateMouseEvent(NULL,
49 eventType,
50 CGPointMake(mouseLoc.x, height - mouseLoc.y),
51 which);
52 CGEventPost(kCGHIDEventTap, click);
53 CFRelease(click);
54 }
55
56 @end