Change two-pass behavior for loading mappings. Allow lazy binding of mappings by...
[enjoyable.git] / Classes / NJOutputKeyPress.m
1 //
2 // NJOutputKeyPress.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "NJOutputKeyPress.h"
9
10 #import "NJKeyInputField.h"
11
12 @implementation NJOutputKeyPress
13
14 + (NSString *)serializationCode {
15 return @"key press";
16 }
17
18 - (NSDictionary *)serialize {
19 return _keyCode != NJKeyInputFieldEmpty
20 ? @{ @"type": self.class.serializationCode, @"key": @(_keyCode) }
21 : nil;
22 }
23
24 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization {
25 NJOutputKeyPress *output = [[NJOutputKeyPress alloc] init];
26 output.keyCode = [serialization[@"key"] intValue];
27 return output;
28 }
29
30 - (void)trigger {
31 if (_keyCode != NJKeyInputFieldEmpty) {
32 CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, _keyCode, YES);
33 CGEventPost(kCGHIDEventTap, keyDown);
34 CFRelease(keyDown);
35 }
36 }
37
38 - (void)untrigger {
39 if (_keyCode != NJKeyInputFieldEmpty) {
40 CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, _keyCode, NO);
41 CGEventPost(kCGHIDEventTap, keyUp);
42 CFRelease(keyUp);
43 }
44 }
45
46 @end