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...
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.
Use this to capture changed flags :
- (void)flagsChanged:(NSEvent *)event
{
}
来源:https://stackoverflow.com/questions/4640696/cocoa-nsevent-respond-to-the-shift-key