问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
In Cocoa, I think you'd want to subclass NSApplication and override -[NSApplication sendEvent:]
.
来源:https://stackoverflow.com/questions/11437345/monitor-keyboard-events-inside-process-process-wide