Provide file promises from the mapping list. Perform various file sanitization in...
[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 _vk != NJKeyInputFieldEmpty
20 ? @{ @"type": self.class.serializationCode, @"key": @(_vk) }
21 : nil;
22 }
23
24 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
25 withMappings:(NSArray *)mappings {
26 NJOutputKeyPress *output = [[NJOutputKeyPress alloc] init];
27 output.vk = [serialization[@"key"] intValue];
28 return output;
29 }
30
31 - (void)trigger {
32 CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, _vk, YES);
33 CGEventPost(kCGHIDEventTap, keyDown);
34 CFRelease(keyDown);
35 }
36
37 - (void)untrigger {
38 CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, _vk, NO);
39 CGEventPost(kCGHIDEventTap, keyUp);
40 CFRelease(keyUp);
41 }
42
43 @end