听Mac键盘上播放/暂停事件 [英] Listening to mac keyboard play/pause events

查看:712
本文介绍了听Mac键盘上播放/暂停事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些Mac的应用程序,如iTunes和Spotify的,反应了播放/暂停/下一首/ $ P $上的一些Apple键盘pvious按钮。

Some mac apps, like iTunes and Spotify, react to the play/pause/next/previous buttons on some Apple keyboards.

presumably,我该怎么做?

Presumably they're tapping into some sort of NSNotification, how can I do the same?

推荐答案

我通过继承的NSApplication(和设置该类作为我的目标的信息应用程序类)具有以下做到这一点:

I do this by subclassing NSApplication (and setting that class as the application class in my target's info) with the following:

#import <IOKit/hidsystem/ev_keymap.h>

...

- (void)mediaKeyEvent:(int)key state:(BOOL)state
{
 switch (key)
 {
  // Play pressed
  case NX_KEYTYPE_PLAY:
   if (state == NO)
    [(TSAppController *)[self delegate] togglePlayPause:self];
   break;

  // FF pressed
  case NX_KEYTYPE_FAST:
   if (state == YES)
    [(TSAppController *)[self delegate] seekForward:self];
   break;

  // RW pressed
  case NX_KEYTYPE_REWIND:
   if (state == YES)
    [(TSAppController *)[self delegate] seekBack:self];
   break;
 }
}

- (void)sendEvent:(NSEvent *)event
{
 // Catch media key events
 if ([event type] == NSSystemDefined && [event subtype] == NX_SUBTYPE_AUX_CONTROL_BUTTONS)
 {
  int keyCode = (([event data1] & 0xFFFF0000) >> 16);
  int keyFlags = ([event data1] & 0x0000FFFF);
  int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;

  // Process the media key event and return
  [self mediaKeyEvent:keyCode state:keyState];
  return;
 }

 // Continue on to super
 [super sendEvent:event];
}

国家在-mediaKeyEvent:状态:是向上/向下。在我的应用程序是有意义的只有播放/暂停按键反应时,它的备份(已完成pressing),但我不断地做出反应RW / FF事件,而最关键的是向下寻求。

The "state" in -mediaKeyEvent:state: is for up/down. In my app it makes sense to only react to the play/pause key when it's back up (done pressing), but I continuously react to RW/FF events while the key is down for seeking.

我很想知道一个更好的办法做到这一点,如果它存在,虽然。目前,除非用户在全球的键盘快捷方式禁用这些键,它控制着我的应用程序的的iTunes中。 : - )

I'd love to know of a better way to do this if it exists, though. Currently, unless the user disables these keys in the global keyboard shortcuts, it controls my app and iTunes. :-)

这code已使用很长一段时间在我的转录应用和行之有效的(除了上述全球键盘快捷键的问题)

This code has been used quite awhile in my transcription app and works well (aside from the global keyboard shortcut issue above).

这篇关于听Mac键盘上播放/暂停事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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