有没有办法使我的应用程序响应Mac上的播放/暂停按钮?
编辑:
使用建议的代码,我得到这个控制台消息:
Could not connect the action buttonPressed: to target of class NSApplication
为什么会这样?
解决方法
我通过对NSApplication进行子类化(并将应用程序的主体类设置为此子类)在我自己的应用程序中完成了此操作.它捕获查找和播放/暂停键,并将其转换为我的应用程序代理中的特定操作.
相关行:
#import <IOKit/hidsystem/ev_keymap.h> - (void)sendEvent:(NSEvent *)event { // Catch media key events if ([event type] == NSSystemDefined && [event subtype] == 8) { 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]; } - (void)mediaKeyEvent:(int)key state:(BOOL)state { switch (key) { // Play pressed case NX_KEYTYPE_PLAY: if (state == NO) [(TSAppController *)[self delegate] togglePlayPause:self]; break; // Rewind case NX_KEYTYPE_FAST: if (state == YES) [(TSAppController *)[self delegate] seekForward:self]; break; // PrevIoUs case NX_KEYTYPE_REWIND: if (state == YES) [(TSAppController *)[self delegate] seekBack:self]; break; } }