Fix old check for unset field.
[enjoyable.git] / 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 @{ @"type": @"key press", @"key": @(_vk) };
20 }
21
22 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
23 withMappings:(NSArray *)mappings {
24 NJOutputKeyPress *output = [[NJOutputKeyPress alloc] init];
25 output.vk = [serialization[@"key"] intValue];
26 return output;
27 }
28
29 - (void)trigger {
30 CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, _vk, YES);
31 CGEventPost(kCGHIDEventTap, keyDown);
32 CFRelease(keyDown);
33 }
34
35 - (void)untrigger {
36 CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, _vk, NO);
37 CGEventPost(kCGHIDEventTap, keyUp);
38 CFRelease(keyUp);
39 }
40
41 @end