台式电脑上的 CGEventCreateKeyboardEvent 与 MacBook 上的 CGEventCreateKeyboardEvent

发布于 2024-09-25 09:41:25 字数 2033 浏览 1 评论 0原文

Ola 伙计们,

我想再一次从使用 SO 的人们共享的知识库中汲取知识。

我为 OSX 编写了一个小应用程序,它将关键事件发送到应用程序。我的目标是 OSX 10.5.x 及更高版本。但是,当我构建 10.6.x 时,问题也存在。一切工作正常,除了当我只发送修饰键时; Alt、Command、Control 和 Shift。

问题在于,在两台 MacBook 上,只要测试人员使用鼠标移动光标或触摸触摸板,修饰键的事件就会被清除。

在安装了 XCode 的桌面上,一切正常。就像它应该的那样。在两台不同的 MacBook 上,都会出现该问题。桌面配有标准 101 键键盘和多按钮鼠标。
当鼠标连接到 MacBook 时,使用带滚轮的两键鼠标。然而,当没有连接外围设备并且使用触摸板时,就会出现问题。

我期望发生的情况是将修改键事件发送到目标应用程序,用户使用鼠标/触摸板移动光标,按下鼠标/触摸板上的按钮和/或使用修改键按下事件按下键盘上的按键'积极的'。然后,当它们完成时,发送修饰键的按键事件。

以下是我发送按键按下事件(本例中为 Shift 键)的方式:

case ModKeyShiftDown:
    xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
    xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, true);
    CGEventSetFlags(xTheCommand, kCGEventFlagMaskAlternate);
    CGEventPost(kCGHIDEventTap, xTheCommand);
    //CGEventPost(kCGSessionEventTap, xTheCommand);
    //CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
    CFRelease(xTheCommand);
    CFRelease(xEventSource);
break;

我使用了所有三个标志来创建事件源(kCGEventSourceStatePrivate、kCGEventSourceStateCombinedSessionState 和 kCGEventSourceStateHIDSystemState)。

我尝试使用事件源以及 null 作为第一个参数创建键盘事件。

我尝试过在事件上使用和不使用适当的标志。

我尝试过各种发布事件的组合; kCGIDEventTap、kCGSessionEventTap 和 kCGAnnotatedSessionEventTap。

为了完整起见,以下是我发送 Shift 键的向上事件的方法:

case ModKeyShiftUp:
    xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
    xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, false);
    CGEventSetFlags(xTheCommand, 0);
    CGEventPost(kCGHIDEventTap, xTheCommand);
    //CGEventPost(kCGSessionEventTap, xTheCommand);
    //CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
    CFRelease(xTheCommand);
    CFRelease(xEventSource);
break;

当测试人员触发修饰键按下事件时,他们可以看到光标按预期发生变化。这让我知道目标应用程序正在处理该事件。然而,一旦他们触摸鼠标或触摸板,光标就会变回标准光标,并且处理鼠标事件,就好像没有激活的修改键事件一样。

我想知道我发送事件的方式是否有问题。我还想知道是否有另一种方法来发送“Going To Work”修饰键事件。

抱歉,如果我说得太多了。我的借口是我只睡了几个小时。 :P

谢谢

-isdi-

Ola Folks,

Once again I want to drink from the pool of knowledge shared by people using SO.

I have written a small app for OSX that sends key events to an application. I am targeting OSX 10.5.x and newer. However, the problem exists when I build for 10.6.x as well. Everything works fine except when I send only the modifier keys; Alt, Command, Control and Shift.

The problem is that on the two MacBooks, the events for the modifier keys appear to be cleared as soon as the testers move the cursor using the mouse or touch the touchpad.

On a desktop with XCode installed, everything works fine. Just like it should. On two different MacBooks, the problem occurs. The desktop has a standard 101 key keyboard and a multi-button mouse attached.
When a mouse is connected to the MacBooks, a two button mouse with scrollwheel is used. However, the problem exists when no peripherals are attached and the touchpad is used.

What I expect to happen is that the Modifier Key event is sent to the target application, the user moves the cursor using the mouse / touchpad, press buttons on the mouse / touchpad and/or press keys on the keyboard with the modifier key down event 'active'. Then, when they finish, the key up event for the modifier key is sent.

Here is how I am sending key down events, the Shift key for this example:

case ModKeyShiftDown:
    xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
    xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, true);
    CGEventSetFlags(xTheCommand, kCGEventFlagMaskAlternate);
    CGEventPost(kCGHIDEventTap, xTheCommand);
    //CGEventPost(kCGSessionEventTap, xTheCommand);
    //CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
    CFRelease(xTheCommand);
    CFRelease(xEventSource);
break;

I have used all three flags for creating the event source (kCGEventSourceStatePrivate, kCGEventSourceStateCombinedSessionState and kCGEventSourceStateHIDSystemState).

I have tried Creating the Keyboard Event with the Event Source as well as null as the first parameter.

I have tried with and without the appropriate flags on the event.

I have tried various combinations of posting the event; kCGHIDEventTap, kCGSessionEventTap and kCGAnnotatedSessionEventTap.

For completeness, here is how I send an up event for the Shift key:

case ModKeyShiftUp:
    xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
    xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, false);
    CGEventSetFlags(xTheCommand, 0);
    CGEventPost(kCGHIDEventTap, xTheCommand);
    //CGEventPost(kCGSessionEventTap, xTheCommand);
    //CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
    CFRelease(xTheCommand);
    CFRelease(xEventSource);
break;

When the testers trigger a modifier key down event, they can see the cursor change as expected. This lets me know the event is being processed by the target application. However, as soon as they touch the mouse or touchpad, the cursor changes back to a standard cursor and the mouse events are processed as if no modifier key events are active.

I would like to know if there is a problem with the way I am sending the events. I would also like to know if there is an alternate way to send Modifier Key events that is Going To Work.

Sorry if I overtalked this. My excuse is that I only slept a couple of hours. :P

Thanx

-isdi-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2024-10-02 09:41:25

Ola,

有趣的是,使用 AXUIElementRef 和 A​​XUIElementPostKeyboardEvent 似乎有效。

无论出于何种原因,使用上面的 Accessibility 对象和方法发送按键事件可以解决测试人员的问题。

Ola,

Interestingly enough, using AXUIElementRef and AXUIElementPostKeyboardEvent seem to work.

For whatever reason, sending key events using the Accessibility object and method above solves the problem for the testers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文