问题描述
我试图模仿cmd标签键盘快捷方式的功能,用户可以在应用程序之间切换某个键,然后当他们释放命令的时候发生。
I'm trying to mimic the functionality of the cmd-tab keyboard shortcut where the user can switch between applications hitting a certain key and then when they release command something happens.
我现在使用这个代码,但它只能检测keydown。我需要这个在键上发射
I'm using this code right now but it can only detects keydown. I need this to fire on key up
- (void)flagsChanged:(NSEvent *)theEvent {
if ([theEvent modifierFlags] & NSCommandKeyMask) {
NSLog(@"Do my stuff here");
}
}
感谢
推荐答案
根据文档:
你需要在这里做的是当你得到其中命令键失效的事件,你需要在某个地方设置一个标志,并在随后的调用中,检查命令键是否失效。
What you need to do here is when you get the event in which the command key goes down, you need to set a flag somewhere, and in subsequent calls, check for the absence of the command key being down.
例如,假设你有一个名为 _cmdKeyDown
的ivar:
For instance, assuming you have an ivar called _cmdKeyDown
:
- (void)flagsChanged:(NSEvent *)theEvent
{
[super flagsChanged:theEvent];
NSUInteger f = [theEvent modifierFlags];
BOOL isDown = !!(f & NSCommandKeyMask);
if (isDown != _cmdKeyDown)
{
NSLog(@"State changed. Cmd Key is: %@", isDown ? @"Down" : @"Up");
_cmdKeyDown = isDown;
}
}
这篇关于Command-Key-Up可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!