【问题标题】:NSEvent commandKey not detectedNSEvent commandKey 未检测到
【发布时间】:2013-01-24 18:25:05
【问题描述】:

我正在尝试检测是否按下了命令键,并且我一生都无法弄清楚以下内容有什么问题。我重写了一个视图以提供以下代码:

- (void)flagsChanged:(NSEvent *)theEvent {

    NSLog(@"flags changed in %@", self);
    BOOL commandKeyPressed = ([theEvent modifierFlags] & NSCommandKeyMask);

    if (commandKeyPressed)
        NSLog(@"command key in %@", self);
}

每当我按下命令键时,我都会看到“标志已更改”消息,但看不到“命令键输入”消息。我错过了什么?

【问题讨论】:

    标签: objective-c macos nsevent


    【解决方案1】:

    BOOLsigned char,因此当您将 int 转换为 BOOL 时,除了低 8 位之外,您将去掉所有位。在您的情况下,非零位不在低 8 位中。相反,说

    BOOL commandKeyPressed = ([theEvent modifierFlags] & NSCommandKeyMask) != 0;
    

    或者只是

    if ([theEvent modifierFlags] & NSCommandKeyMask)
    

    【讨论】:

    • 现在是:BOOL commandKeyPressed = (theEvent.modifierFlags & NSEventModifierFlagCommand) != 0; NSCommandKeyMask 已弃用。
    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2018-12-11
    • 2019-11-25
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多