Cocoa - NSEvent Respond to the SHIFT key?

懵懂的女人 提交于 2019-11-29 11:55:51

Take a look at the flagsChanged: method of NSResponder.

Something like this:

- (void) flagsChanged:(NSEvent *) event {
    if ([event modifierFlags] & NSShiftKeyMask) {
        //Do something
    }
}

Your code sample isn't working because -modifierFlags is a bitmask and so testing whether the mask is equal to NSShiftKeyMask won't work. You need to use the bitwise AND operator to test if the flag is set:

if ([event modifierFlags] & NSShiftKeyMask) {
    NSLog(@"Shift key pressed");
}

Also as an aside if you want to store this result in a BOOL you need to check that it is not equal to 0, such as:

BOOL shiftKeyPressed = ([event modifierFlags] & NSShiftKeyMask) != 0;

If you leave it out then the BOOL will evaluate to NO (unless the value you pull out of the bitmask is 1 which is defined as YES; for -modifierFlags this won't happen as the masks start at 1 << 16).

Here is my solution to detecting modifier key changes:

- (void)keyboard:(AMKeyboardView *)keyboard flagsChanged:(NSEvent *)event {
    if ((event.modifierFlags & NSShiftKeyMask) && !lastShiftState) {
        lastShiftState = true;
        //Shift pressed - do something
    }
    else if (!(event.modifierFlags & NSShiftKeyMask) && lastShiftState) {
        lastShiftState = false;
        //Shift released - do something
    }

    else if ((event.modifierFlags & NSFunctionKeyMask) && !lastFnState) {
        lastFnState = true;
        //Fn pressed - do something
    }
    else if (!(event.modifierFlags & NSFunctionKeyMask) && lastFnState) {
        lastFnState = false;
        //Fn released - do something
    }

    else if ((event.modifierFlags & NSControlKeyMask) && !lastControlState) {
        lastControlState = true;
        //Control pressed - do something
    }
    else if (!(event.modifierFlags & NSControlKeyMask) && lastControlState) {
        lastControlState = false;
        //Control released - do something
    }


    else if ((event.modifierFlags & NSAlternateKeyMask) && !lastOptionState) {
        lastOptionState = true;
        //Option pressed - do something
    }
    else if (!(event.modifierFlags & NSAlternateKeyMask) && lastOptionState) {
        lastOptionState = false;
        //Option released - do something
    }

    else if ((event.modifierFlags & NSCommandKeyMask) && !lastCommandState) {
        lastCommandState = true;
        //Command pressed - do something
    }
    else if (!(event.modifierFlags & NSCommandKeyMask) && lastCommandState) {
        lastCommandState = false;
        //Command released - do something
    }

    else NSLog(@"Other");
}

It requires 4 instance variables to store previous states, but can detect the press and release of every modifier key.

The Shift key doesn't have a key code, since it's a key modifier. When you get a key event, the presence of the Shift key will be in the modifierFlags field.

HappyGL

Use this to capture changed flags :

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!