Split view management out of NJDeviceController. Right now this probably just makes...
[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 withMappings:(NSArray *)mappings {
26 NJOutputKeyPress *output = [[NJOutputKeyPress alloc] init];
27 output.keyCode = [serialization[@"key"] intValue];
28 return output;
29 }
30
31 - (void)trigger {
32 if (_keyCode != NJKeyInputFieldEmpty) {
33 CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, _keyCode, YES);
34 CGEventPost(kCGHIDEventTap, keyDown);
35 CFRelease(keyDown);
36 }
37 }
38
39 - (void)untrigger {
40 if (_keyCode != NJKeyInputFieldEmpty) {
41 CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, _keyCode, NO);
42 CGEventPost(kCGHIDEventTap, keyUp);
43 CFRelease(keyUp);
44 }
45 }
46
47 @end