Clean up a bunch of properties; remove unused ones, use automatic ones where possible...
[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 + (NSString *)serializationCode {
14 return @"mbtn";
15 }
16
17 - (NSDictionary *)serialize {
18 return @{ @"type": @"mbtn", @"button": @(_button) };
19 }
20
21 + (Target *)targetDeserialize:(NSDictionary *)serialization
22 withConfigs:(NSArray *)configs {
23 TargetMouseBtn *target = [[TargetMouseBtn alloc] init];
24 target.button = [serialization[@"button"] intValue];
25 return target;
26 }
27
28 -(void) trigger {
29 NSRect screenRect = [[NSScreen mainScreen] frame];
30 NSInteger height = screenRect.size.height;
31 NSPoint mouseLoc = [NSEvent mouseLocation];
32 CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
33 CGEventRef click = CGEventCreateMouseEvent(NULL,
34 eventType,
35 CGPointMake(mouseLoc.x, height - mouseLoc.y),
36 _button);
37 CGEventPost(kCGHIDEventTap, click);
38 CFRelease(click);
39 }
40
41 -(void) untrigger {
42 NSRect screenRect = [[NSScreen mainScreen] frame];
43 NSInteger height = screenRect.size.height;
44 NSPoint mouseLoc = [NSEvent mouseLocation];
45 CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
46 CGEventRef click = CGEventCreateMouseEvent(NULL,
47 eventType,
48 CGPointMake(mouseLoc.x, height - mouseLoc.y),
49 _button);
50 CGEventPost(kCGHIDEventTap, click);
51 CFRelease(click);
52 }
53
54 @end