事件点击:使用CGEventPost,kCGSessionEventTap,kCGAnnotatedSessionEventTap,CGEventTapPostEvent改变结果 [英] Event taps: Varying results with CGEventPost, kCGSessionEventTap, kCGAnnotatedSessionEventTap, CGEventTapPostEvent

查看:279
本文介绍了事件点击:使用CGEventPost,kCGSessionEventTap,kCGAnnotatedSessionEventTap,CGEventTapPostEvent改变结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过事件水龙头发布事件时遇到了一个棘手的问题.我在kCGHIDEventTap上点击NSSystemDefined,然后将事件替换为新事件.我遇到的问题是,取决于我发布事件的方式,只有某些应用程序才能看到它.我的测试应用程序是Opera,Firefox,Quicksilver和Xcode.这是我在事件点击回调中尝试使用的不同技术,并产生了结果.我期望每个应用都有一个动作(正确响应"); 系统提示音"表示没有任何键绑定到系统声音.

I'm running into a thorny problem with posting an event from an event tap. I'm tapping for NSSystemDefined at kCGHIDEventTap, then replacing the event with a new one. The problem I'm running in to is that depending on how I post the event, it's being seen only by some applications. My test applications are Opera, Firefox, Quicksilver, and Xcode. Here are the different techniques I've tried within my event tap callback, with results. I'm expecting an action (the "correct response") from each app; "system beep" means the nothing-is-bound-to-that-key system sound.

  1. 创建一个新事件,并从回调中返回它. Opera:无响应/系统提示音,Firefox:无响应/系统提示音,Quicksilver:正确响应,Xcode:无响应/系统提示音

  1. Create a new event, and return it from the callback. Opera: no response/system beep, Firefox: no response/system beep, Quicksilver: correct response, Xcode: no response/system beep

创建一个新事件,使用CGEventPost发布到kCGSessionEventTap,返回null. Opera:无响应/系统提示音,Firefox:无响应/系统提示音,Quicksilver:正确响应,Xcode:无响应/系统提示音

Create a new event, post to kCGSessionEventTap with CGEventPost, return null. Opera: no response/system beep, Firefox: no response/system beep, Quicksilver: correct response, Xcode: no response/system beep

创建一个新事件,并使用CGEventPost发布到kCGAnnotatedSessionEventTap,返回null. Opera:正确响应,Firefox:正确响应,Quicksilver:无响应/系统提示音,Xcode:无响应/系统提示音

Create a new event, post to kCGAnnotatedSessionEventTap with CGEventPost, return null. Opera: correct response, Firefox: correct response, Quicksilver: no response/system beep, Xcode: no response/system beep

创建一个新事件,使用CGEventTapPostEvent发布,返回null. Opera:无响应/系统提示音,Firefox:无响应/系统提示音,Quicksilver:正确响应,Xcode:无响应/系统提示音

Create a new event, post with CGEventTapPostEvent, return null. Opera: no response/system beep, Firefox: no response/system beep, Quicksilver: correct response, Xcode: no response/system beep

创建一个新事件,使用CGEventPost发布到kCGSessionEventTap,并返回新事件. Opera:无响应/系统提示音,Firefox:无响应/系统提示音,Quicksilver:正确响应,Xcode:无响应/系统提示音

Create a new event, post to kCGSessionEventTap with CGEventPost, and return new event. Opera: no response/system beep, Firefox: no response/system beep, Quicksilver: correct response, Xcode: no response/system beep

创建一个新事件,使用CGEventPost发布到kCGAnnotatedSessionEventTap,然后返回新事件. Opera:正确的响应和系统蜂鸣声,Firefox:正确的响应和系统蜂鸣声,Quicksilver:正确的响应和系统蜂鸣声,Xcode:无响应/系统两次蜂鸣声

Create a new event, post to kCGAnnotatedSessionEventTap with CGEventPost, and return new event. Opera: correct response and system beep, Firefox: correct response and system beep, Quicksilver: correct response and system beep, Xcode: no response/double system beep

创建一个新事件,使用CGEventTapPostEvent发布,并返回新事件. Opera:无响应/系统提示音,Firefox:无响应/系统提示音,Quicksilver:正确响应,Xcode:无响应/系统提示音

Create a new event, post with CGEventTapPostEvent, and return new event. Opera: no response/system beep, Firefox: no response/system beep, Quicksilver: correct response, Xcode: no response/system beep

(6)是最好的,但是用户抱怨正确响应会产生额外的系统提示音,我猜这是来自该事件的双重发布.我不确定要尝试其他组合还是在其他地方寻找.谁能提供任何指导?有什么办法既可以从我的回调中返回事件,又可以将事件发布到带注释的水龙头,而无需同时执行这两项操作?

(6) is the best, but users are complaining about the extra system beep on correct responses, which I'm guessing is coming from the double-posting of the event. I'm not sure of other combinations to try, or where else to look. Can anyone offer any guidance? Is there any way to get the results of both returning the event from my callback and posting to the annotated tap without doing both?

很抱歉,这个冗长的问题;我已经做了很多实验.

Sorry for the lengthy question; I've been doing a lot of experimenting.

预先感谢

更新:这是我用来创建事件点击的代码:

Update: this is the code I use to create the event tap:

CFMachPortRef eventTap;
eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0,CGEventMaskBit(NX_SYSDEFINED) | (1 << kCGEventKeyDown) | (1 << kCGEventKeyUp), myCGEventCallback, (void *)hidEventQueue);

推荐答案

我认为我已解决此问题.我一直在使用+[NSEvent keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:]创建一个NSEvent,然后返回该事件的-CGEvent.我切换到CGEventCreateKeyboardEvent,使用通过CGEventCreateSourceFromEvent从原始事件创建的事件源,并从回调返回该事件.我所有的测试现在都通过了.

I think I fixed this. I had been using +[NSEvent keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:] to create an NSEvent, then returning that event's -CGEvent. I switched to CGEventCreateKeyboardEvent, using the event source created from the original event with CGEventCreateSourceFromEvent, and returning the event from the callback. All my tests pass now.

这篇关于事件点击:使用CGEventPost,kCGSessionEventTap,kCGAnnotatedSessionEventTap,CGEventTapPostEvent改变结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆