Command-Key-Up可可 [英] Command-Key-Up Cocoa

查看:255
本文介绍了Command-Key-Up可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿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");
}
}

感谢

推荐答案

根据文档:


通知接收方用户已按下或已发布修饰符
键(Shift,Control等)。

Informs the receiver that the user has pressed or released a modifier key (Shift, Control, and so on).

你需要在这里做的是当你得到其中命令键失效的事件,你需要在某个地方设置一个标志,并在随后的调用中,检查命令键是否失效。

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可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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