CGEventTapCreate神秘地使用“按键”事件 [英] CGEventTapCreate breaks down mysteriously with "key down" events

查看:659
本文介绍了CGEventTapCreate神秘地使用“按键”事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CGEventTapCreate 在我的应用程序运行时从iTunes窃取媒体密钥。传递给 CGEventTapCreate 的回调中的代码检查事件,如果发现它是媒体键之一,则向默认通知中心发布适当的通知。

I'm using CGEventTapCreate to "steal" media keys from iTunes when my app is running. The code inside of the callback that I pass to CGEventTapCreate examines the event, and if it finds that it's one of the media keys, posts an appropriate notification to the default notification center.

现在,如果我发布一个密钥向上事件的通知,这很好。如果我这样做的关键事件,最终我的应用程序停止获取媒体键事件和iTunes接管。任何想法,什么可以导致这一点?代码的相关部分位于

Now, this works fine if I post a notification for the "key up" event. If I do that for "key down" events, eventually my app stops getting media key events and iTunes takes over. Any ideas on what can be causing this? The relevant part of the code is below

enum { 
...
  PlayPauseKeyDown = 0x100A00,
  PlayPauseKeyUp = 0x100B00,
...
};

static CGEventRef event_tap_callback(CGEventTapProxy proxy,
                                     CGEventType type,
                                     CGEventRef event,
                                     void *refcon)
{
  if (!(type == NX_SYSDEFINED) || (type == NX_KEYUP) || (type == NX_KEYDOWN))
      return event;

  NSEvent* keyEvent = [NSEvent eventWithCGEvent: event];
  if (keyEvent.type != NSSystemDefined) return event;

  switch(keyEvent.data1)
  {
    case PlayPauseKeyUp:  // <--- this works reliably
    //case PlayPauseKeyDown:  // <--- this will break eventually
      post_notification(@"PlayPauseMediaKeyPressed", nil, nil);
      return NULL;

    ... and so on ...


推荐答案


如果回调需要太长时间,是否会杀死我的事件水龙头?

Does something kill my event tap if the callback takes too long?

人们怀疑雪豹有一个错误,有时会禁用事件敲击,即使他们不需要太长时间。要处理它,您可以观察事件类型 kCGEventTapDisabledByTimeout ,并通过重新启用您的点击与 CGEventTapEnable 响应。

Some people suspect that Snow Leopard has a bug that sometimes disables event taps even if they don't take too long. To handle that, you can watch for the event type kCGEventTapDisabledByTimeout, and respond by re-enabling your tap with CGEventTapEnable.

这篇关于CGEventTapCreate神秘地使用“按键”事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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