Cocoa - NSEvent Respond to the SHIFT key?

一笑奈何 提交于 2019-12-18 06:58:11

问题


I was wondering if NSEvent responds to the "Shift" key on the keyboard. I am logging the keyCodes when debugging my app and I don't get a keyCode value for the shift key.

Thanks,

Kevin

EDIT: This is the code I am using from a user response.

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

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

The Shift key is still not being recognized...


回答1:


Take a look at the flagsChanged: method of NSResponder.

Something like this:

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



回答2:


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).




回答3:


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.




回答4:


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.




回答5:


Use this to capture changed flags :

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

}


来源:https://stackoverflow.com/questions/4640696/cocoa-nsevent-respond-to-the-shift-key

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