让我的Cocoa应用程序响应键盘的播放/暂停键? [英] Make my Cocoa app respond to the keyboard play/pause key?
本文介绍了让我的Cocoa应用程序响应键盘的播放/暂停键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让我的应用程式回应Mac上的播放/暂停按钮?
编辑: b
$ b
使用建议的代码,我得到此控制台消息:
无法连接操作按钮按下:到目标NSApplication
为什么会这样?
方案
我通过子类化NSApplication(并将应用程序的主类设置为此子类)在我自己的应用程序中实现了这一点。
相关行:
pre> #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;
//处理媒体键事件并返回
[self mediaKeyEvent:keyCode state:keyState];
return;
}
//继续到super
[super sendEvent:event];
}
- (void)mediaKeyEvent:(int)key state:(BOOL)state
{
switch(key)
{
//按下
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;
//上一页
case NX_KEYTYPE_REWIND:
if(state == YES)
[(TSAppController *)[self delegate] seekBack:self];
break;
}
}
Is there a way to make my app respond to the play/pause button on Mac?
EDIT:
Using the suggested code,I get this console message:
Could not connect the action buttonPressed: to target of class NSApplication
Why would that be?
解决方案
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.
Relevant lines:
#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;
}
}
这篇关于让我的Cocoa应用程序响应键盘的播放/暂停键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文