8

I can recognize when the user presses any Shift key with this code:

-(void)flagsChanged:(NSEvent *)theEvent
{
    if ([theEvent modifierFlags] & NSShiftKeyMask)
        //. . .
}

but is there any way to distinguish whether it was the right or left Shift key that was pressed?

jscs
  • 63,694
  • 13
  • 151
  • 195

3 Answers3

8

You can do it like this:

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

    if ([theEvent modifierFlags] == 131330) {
        NSLog(@"Left shift pressed!");
    }

    if ([theEvent modifierFlags] == 131332) {
        NSLog(@"Right shift pressed!");
    }
}
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • 7
    @user437064 Download this free app *Key Codes* from here: http://manytricks.com/keycodes/. Here You can see all key Codes and modifiers. **Note:** For letters You need to use Key Codes. – Justin Boo May 23 '12 at 10:36
3

In Swift:

let isLeftShift = event.modifierFlags.rawValue & UInt(NX_DEVICELSHIFTKEYMASK) != 0
let isRightShift = event.modifierFlags.rawValue & UInt(NX_DEVICERSHIFTKEYMASK) != 0
mohd.akram
  • 121
  • 2
  • 3
-1
static __INLINE void i_modifier_flags(
                        NSUInteger flags,
                        bool_t *rshift, bool_t *rctrl, bool_t *rcommand, bool_t *ralt,
                        bool_t *lshift, bool_t *lctrl, bool_t *lcommand, bool_t *lalt)
{
    *rshift = ((flags & 131332) == 131332) ? TRUE : FALSE;
    *rctrl = ((flags & 270592) == 270592) ? TRUE : FALSE;
    *rcommand = ((flags & 1048848) == 1048848) ? TRUE : FALSE;
    *ralt = ((flags & 524608) == 524608) ? TRUE : FALSE;
    *lshift = ((flags & 131330) == 131330) ? TRUE : FALSE;
    *lctrl = ((flags & 262401) == 262401) ? TRUE : FALSE;
    *lcommand = ((flags & 1048840) == 1048840) ? TRUE : FALSE;
    *lalt = ((flags & 524576) == 524576) ? TRUE : FALSE;
}
frang
  • 69
  • 5