【问题标题】:Make my Cocoa app respond to the keyboard play/pause key?让我的 Cocoa 应用程序响应键盘播放/暂停键?
【发布时间】:2011-04-22 13:37:28
【问题描述】:

有没有办法让我的应用响应 Mac 上的播放/暂停按钮?

编辑:

使用建议的代码,我收到以下控制台消息:

无法将操作 buttonPressed: 连接到 NSApplication 类的目标

为什么会这样?

【问题讨论】:

    标签: objective-c cocoa macos events keyboard


    【解决方案1】:

    我在自己的应用程序中通过子类化 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;
        }
    }
    

    编辑:

    斯威夫特 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
        }
    
    }
    

    【讨论】:

    • 您遇到的具体问题是什么?我已经在一个已经推出几年的运输应用程序中实现了这一点。
    • 我能够让它工作,但我仍然有一个问题,iTunes 启动并且在我按下按键时仍然会播放/暂停。有解决办法吗?
    • 运行 iTunes 的相同问题(Hot to prevent iTunes from Launching?Spotify 已经处理了这个问题。甚至更多:它只需要接触。我的应用程序的代码来自这个答案也没有收到事件当 Spotify 运行时这些按钮。
    • 只是为了让帖子更新:在 Swift 中提供相同的代码(并且与 Swift 2 兼容,经过测试)here
    • 在媒体按键上处理 iTunes 启动。 github.com/nevyn/SPMediaKeyTap
    【解决方案2】:

    这里有一篇关于这个主题的精彩文章:http://www.rogueamoeba.com/utm/2007/09/29/

    【讨论】:

      【解决方案3】:

      检查这个:https://gist.github.com/gauravk92/546311 完美运行。

      例如,这个 repo 使用它:https://github.com/onepill/PauseIt

      【讨论】:

      【解决方案4】:

      如果有人来看,有sample code to do this here。这种方法确实允许您“吃掉”它们没有到达 iTunes 或其他媒体密钥感知应用程序的事件。

      但请注意,沙盒不允许点击事件,因此这在 App Store 中不起作用。如果有人有解决方法我很乐意听到。

      【讨论】:

        猜你喜欢
        • 2011-11-20
        • 2017-07-10
        • 2011-01-29
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多