Fix memory leak.
[enjoyable.git] / TargetKeyboard.m
1 //
2 // TargetKeyboard.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "TargetKeyboard.h"
9
10 #import "KeyInputTextView.h"
11
12 @implementation TargetKeyboard
13
14 @synthesize vk;
15
16 + (NSString *)serializationCode {
17 return @"key";
18 }
19
20 - (NSDictionary *)serialize {
21 return @{ @"type": @"key", @"key": @(self.vk) };
22 }
23
24 + (Target *)targetDeserialize:(NSDictionary *)serialization
25 withConfigs:(NSArray *)configs {
26 TargetKeyboard *target = [[TargetKeyboard alloc] init];
27 target.vk = [serialization[@"key"] intValue];
28 return target;
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 - (NSString *)descr {
44 return [KeyInputTextView stringForKeyCode:self.vk];
45 }
46
47 @end