监听Mac键盘播放/暂停事件

发布于 2024-08-26 07:39:50 字数 121 浏览 4 评论 0原文

某些 Mac 应用程序(例如 iTunes 和 Spotify)会对某些 Apple 键盘上的播放/暂停/下一个/上一个按钮做出反应。

想必他们正在利用某种 NSNotification,我该如何做同样的事情呢?

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

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

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

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

发布评论

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

评论(1

迷途知返 2024-09-02 07:39:50

我通过使用以下内容子类化 NSApplication(并将该类设置为目标信息中的应用程序类)来实现此目的:

#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:state: 中的“状态”用于向上/向下。在我的应用程序中,仅在播放/暂停键备份(按下完毕)时对其做出反应是有意义的,但当按键按下进行查找时,我会不断对 RW/FF 事件做出反应。

不过,我很想知道是否有更好的方法来做到这一点。目前,除非用户在全局键盘快捷键中禁用这些键,否则它会控制我的应用程序和 iTunes。 :-)

这段代码已经在我的转录应用程序中使用了一段时间并且运行良好(除了全局键盘快捷键问题)多于)。

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];
}

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.

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. :-)

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

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