espanso/native/libmacbridge/bridge.mm
2019-09-05 18:34:03 +02:00

57 lines
1.5 KiB
Plaintext

#include "bridge.h"
#import <Foundation/Foundation.h>
#include "AppDelegate.h"
extern "C" {
}
#include <vector>
void register_keypress_callback(void * self, KeypressCallback callback) {
keypress_callback = callback;
interceptor_instance = self;
}
int32_t initialize() {
AppDelegate *delegate = [[AppDelegate alloc] init];
NSApplication * application = [NSApplication sharedApplication];
[application setDelegate:delegate];
}
int32_t eventloop() {
[NSApp run];
}
void send_string(const char * string) {
// Convert the c string to a UniChar array as required by the CGEventKeyboardSetUnicodeString method
NSString * nsString = [NSString stringWithUTF8String:string];
CFStringRef cfString = (__bridge CFStringRef)nsString;
std::vector<UniChar> buffer(nsString.length);
CFStringGetCharacters(cfString, CFRangeMake(0, nsString.length), buffer.data());
// Send the event
CGEventRef e = CGEventCreateKeyboardEvent(NULL, 0x31, true);
CGEventKeyboardSetUnicodeString(e, buffer.size(), buffer.data());
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
}
void delete_string(int32_t count) {
for (int i = 0; i<count; i++) {
CGEventRef keydown;
keydown = CGEventCreateKeyboardEvent (NULL, 0x33, true);
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
usleep(2000);
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent (NULL, 0x33, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
usleep(2000);
}
}