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