Is there a way to differentiate between left and right Shift keys being pressed?
Asked Answered
S

3

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?

Selhorst answered 23/5, 2012 at 8:9 Comment(0)
M
7

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!");
    }
}
Masturbate answered 23/5, 2012 at 8:39 Comment(2)
@Selhorst Download this free app Key Codes from here: manytricks.com/keycodes. Here You can see all key Codes and modifiers. Note: For letters You need to use Key Codes.Masturbate
You should not compare flags using ==. There may be other "flag" keys active at the same time (such as caps lock) that will throw off the comparison.Washbasin
C
3

In Swift:

let isLeftShift = event.modifierFlags.rawValue & UInt(NX_DEVICELSHIFTKEYMASK) != 0
let isRightShift = event.modifierFlags.rawValue & UInt(NX_DEVICERSHIFTKEYMASK) != 0
Carrack answered 24/10, 2018 at 13:38 Comment(1)
Very nice solution.Lair
W
-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;
}
Wychelm answered 19/4, 2020 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.