让我的 Cocoa 应用程序响应键盘播放/暂停键? [英] Make my Cocoa app respond to the keyboard play/pause key?

查看:22
本文介绍了让我的 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 应用程序响应键盘播放/暂停键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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