Big rename part 3: 'target' to 'output'.
[enjoyable.git] / NJOutputKeyPress.m
diff --git a/NJOutputKeyPress.m b/NJOutputKeyPress.m
new file mode 100644 (file)
index 0000000..c2000d8
--- /dev/null
@@ -0,0 +1,41 @@
+//
+//  NJOutputKeyPress.m
+//  Enjoy
+//
+//  Created by Sam McCall on 5/05/09.
+//
+
+#import "NJOutputKeyPress.h"
+
+#import "NJKeyInputField.h"
+
+@implementation NJOutputKeyPress
+
++ (NSString *)serializationCode {
+    return @"key";
+}
+
+- (NSDictionary *)serialize {
+    return @{ @"type": @"key press", @"key": @(_vk) };
+}
+
++ (NJOutput *)outputDeserialize:(NSDictionary *)serialization
+                  withMappings:(NSArray *)mappings {
+    NJOutputKeyPress *output = [[NJOutputKeyPress alloc] init];
+    output.vk = [serialization[@"key"] intValue];
+    return output;
+}
+
+- (void)trigger {
+    CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, _vk, YES);
+    CGEventPost(kCGHIDEventTap, keyDown);
+    CFRelease(keyDown);
+}
+
+- (void)untrigger {
+    CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, _vk, NO);
+    CGEventPost(kCGHIDEventTap, keyUp);
+    CFRelease(keyUp);
+}
+
+@end