Monitor keyboard events inside process (Process Wide)

夙愿已清 提交于 2019-12-10 11:51:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!