我如何知道 CGEventRef 来自哪个应用程序?
我已经成功获得了一个拦截键盘事件的演示应用程序。这是他们的处理程序。
CGEventRef keyUpCallback (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
NSLog(@"KeyUp event tapped!");
return;
}
我想根据发送事件的应用程序执行不同的操作。我如何知道它是哪个应用程序?
I've successfully got a demo app intercepting keyboard events. Here's the handler for them.
CGEventRef keyUpCallback (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
NSLog(@"KeyUp event tapped!");
return;
}
I want to do different things depending on which application sent the event. How can I tell which application it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接收击键的应用程序可能是活动的应用程序,因此您可以根据活动的应用程序以不同的方式处理事件。您可以使用 NSWorkspace 获取活动应用程序的名称。
另请参阅 这个线程是关于获取活动应用程序的。
The application that is receiving keystrokes is presumably the active application, so you could handle the event differently depending on which application is active. You can use the
activeapplication
method from NSWorkspace to get the name of the active application.See also this thread about getting the active application.
具体流程如下:
processIdSource
显示应用程序发送方,processIdTarget
代表接收方。例如,您可以打开虚拟键盘并用它发送事件。由于虚拟键盘是用户空间中的进程,因此您将获得它的 pid 作为
processIdSource
。但在大多数情况下,您将得到0
作为processIdSource
。获得应用程序的 pid 后,您可以创建 NSRunningApplication 实例并从中获取大量信息。
Here how it goes:
processIdSource
shows you application sender andprocessIdTarget
stands for receiver.For example you can open virtual keyboard and send events with it. As virtual keyboard is process in user space you will get it's pid as
processIdSource
. But for most of cases you will get0
asprocessIdSource
.After you got application's pid you can create
NSRunningApplication
instance and get bunch of information from it.这显示发送事件的应用程序的进程 ID:
This shows process ID of the application that sent the event: