Make my Cocoa app respond to the keyboard play/pause key?(让我的 Cocoa 应用程序响应键盘播放/暂停键?)
问题描述
有没有办法让我的应用响应 Mac 上的播放/暂停按钮?
Is there a way to make my app respond to the play/pause button on Mac?
使用建议的代码,我收到以下控制台消息:
Using the suggested code,I get this console message:
无法将操作 buttonPressed: 连接到 NSApplication 类的目标
Could not connect the action buttonPressed: to target of class NSApplication
为什么会这样?
推荐答案
我在自己的应用程序中通过子类化 NSApplication(并将应用程序的主体类设置为这个子类)来实现这一点.它捕获搜索和播放/暂停键并将它们转换为我的应用程序委托中的特定操作.
I accomplished this in my own application by subclassing NSApplication (and setting the app's principal class to this subclass). It catches seek and play/pause keys and translates them to specific actions in my app delegate.
相关行:
#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;
}
}
斯威夫特 4:
override func sendEvent(_ event: NSEvent)
{
if event.type == .systemDefined &&
event.subtype == .screenChanged
{
let keyCode : Int32 = (Int32((event.data1 & 0xFFFF0000) >> 16))
let keyFlags = (event.data1 & 0x0000FFFF)
let keyState = ((keyFlags & 0xFF00) >> 8) == 0xA
self.mediaKeyEvent(withKeyCode: keyCode, andState: keyState)
return
}
super.sendEvent(event)
}
private func mediaKeyEvent(withKeyCode keyCode : Int32, andState state : Bool)
{
guard let delegate = self.delegate as? AppDelegate else { return }
switch keyCode
{
// Play pressed
case NX_KEYTYPE_PLAY:
if state == false
{
delegate.musicPlayerWC.handleUserPressedPlayButton()
}
break
// Rewind
case NX_KEYTYPE_FAST:
if state == true
{
delegate.musicPlayerWC.handleUserPressedNextSongButton()
}
break
// Previous
case NX_KEYTYPE_REWIND:
if state == true
{
delegate.musicPlayerWC.handleUserPressedPreviousSongButton()
}
break
default:
break
}
}
这篇关于让我的 Cocoa 应用程序响应键盘播放/暂停键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:让我的 Cocoa 应用程序响应键盘播放/暂停键?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01