I can install system wide keyboard monitor by the below instructions:
CGEventRef eventCallback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void *userData)
{
}
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap, kCGEventTapOptionDefault,
kCGEventKeyDown,
&eventCallback,
NULL);
if(eventTap)
{
CFRunLoopSourceRef eventRunLoopSourceRef =
CFMachPortCreateRunLoopSource(NULL, eventTap, 0);
CFRelease(eventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(), eventRunLoopSourceRef,
kCFRunLoopDefaultMode);
CFRelease(eventRunLoopSourceRef);
}
The disadvantage of this code is that it requires to activate "Universal access" in "System Preferences" and also monitor all processes (I do not need it).
I want to monitor keyboard events inside my process. How it possible and is it required to activate "Universal access"? Thankyou.
I think you want NSEvent
's addLocalMonitorForEventsMatchingMask:handler:
self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:^(NSEvent *event) {
NSLog( @"keyDown event!" );
return event;
}];
See the docs. This doesn't require Universal Access to be turned on.