监视进程内的键盘事件(Process Wide) [英] Monitor keyboard events inside process (Process Wide)
本文介绍了监视进程内的键盘事件(Process Wide)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过以下指令安装系统范围的键盘监视器:
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.
推荐答案
我想你想要 NSEvent
$ c> addLocalMonitorForEventsMatchingMask:handler:
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.
这篇关于监视进程内的键盘事件(Process Wide)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文