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