2019-09-05 15:20:52 +00:00
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
// 10.9+ only, see this url for compatibility:
|
|
|
|
// http://stackoverflow.com/questions/17693408/enable-access-for-assistive-devices-programmatically-on-10-9
|
|
|
|
BOOL checkAccessibility()
|
|
|
|
{
|
|
|
|
NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES};
|
|
|
|
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts);
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:34:03 +00:00
|
|
|
KeypressCallback keypress_callback;
|
|
|
|
void * interceptor_instance;
|
|
|
|
|
2019-09-05 15:20:52 +00:00
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
if (checkAccessibility()) {
|
|
|
|
NSLog(@"Accessibility Enabled");
|
|
|
|
}else {
|
|
|
|
NSLog(@"Accessibility Disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog(@"registering keydown mask");
|
|
|
|
[NSEvent addGlobalMonitorForEventsMatchingMask:(NSEventMaskKeyDown | NSEventMaskFlagsChanged)
|
|
|
|
handler:^(NSEvent *event){
|
2019-09-05 17:18:55 +00:00
|
|
|
if (event.type == NSEventTypeKeyDown
|
|
|
|
&& event.keyCode != 0x33) { // Send backspace as a modifier
|
|
|
|
|
2019-09-05 16:34:03 +00:00
|
|
|
const char * chars = [event.characters UTF8String];
|
|
|
|
int len = event.characters.length;
|
2019-09-05 17:18:55 +00:00
|
|
|
|
|
|
|
keypress_callback(interceptor_instance, chars, len, 0, event.keyCode);
|
|
|
|
//NSLog(@"keydown: %@, %d", event.characters, event.keyCode);
|
2019-09-05 15:20:52 +00:00
|
|
|
}else{
|
2019-09-05 17:18:55 +00:00
|
|
|
// Because this event is triggered for both the press and release of a modifier, trigger the callback
|
|
|
|
// only on release
|
|
|
|
if (([event modifierFlags] & (NSEventModifierFlagShift | NSEventModifierFlagCommand |
|
|
|
|
NSEventModifierFlagControl | NSEventModifierFlagOption)) == 0) {
|
|
|
|
|
|
|
|
keypress_callback(interceptor_instance, NULL, 0, 1, event.keyCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
//NSLog(@"keydown: %d", event.keyCode);
|
2019-09-05 15:20:52 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|