Monitor keyboard events inside process (Process Wide)
-
20-06-2021 - |
Domanda
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.
Soluzione
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.
Altri suggerimenti
If you do not have to monitor other processes then you should be able to use normal events to look at the keyboard.
In Carbon, install a handler for kEventRawKeyDown
(say) of kEventClassKeyboard
, e.g. at the application or window levels.
In Cocoa, implement keyDown:
or whatever method you require on a subclass of NSResponder
such as your NSApplication
subclass or a particular NSWindow
subclass.
ProcessSerialNumber psn = { 0 };
GetCurrentProcess( & psn );
CGEventTapCreateForPSN( & psn, ... );
This is the process-wide manner for listening to events only in the current process, not system-wide.
In Cocoa, I think you'd want to subclass NSApplication and override -[NSApplication sendEvent:]
.