8
votes

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?

3

3 Answers

7
votes

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!");
    }
}
2
votes

In Swift:

let isLeftShift = event.modifierFlags.rawValue & UInt(NX_DEVICELSHIFTKEYMASK) != 0
let isRightShift = event.modifierFlags.rawValue & UInt(NX_DEVICERSHIFTKEYMASK) != 0
-1
votes
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;
}