Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSEvent modifier flags -- bug when both Shift and Caps-Lock are held down?

I have an NSEvent Keyboard hook callback. I'm looking at the event's modifier flags to tell if the letter should be capitalized.

When Caps-Lock is turned on and shift is held down, and you press a key... that key comes out as a capital letter but both SHIFT and CAPS flags return FALSE.

//For testing which flags are on.
//Holding down Shift and Caps for some reason = FALSE FALSE...
NSUInteger flags = [NSEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
if( flags == NSShiftKeyMask ){
    NSLog(@"Shift - TRUE");
} else {
    NSLog(@"Shift - FALSE");
}

if( flags == NSAlphaShiftKeyMask ){
    NSLog(@"CAPS - TRUE");
} else {
    NSLog(@"CAPS - FALSE");
}
return newUserKeypress;

So,

-Caps-Lock is ON (the light is on)

-Shift is being held down

-Tap [e] key

-Output is "E"

-But output of the above code is FALSE FALSE.

Using either shift OR caps correctly reports the values. Why isn't having both of them on reporting correctly? And if both of them are off... why is the letter still being capitalized?

If this is for some reason correct... how can I tell a normal keypress apart from a keypress with shift and caps held down? (they have the same FALSE-FALSE flags)

like image 459
ck_ Avatar asked Dec 28 '22 14:12

ck_


1 Answers

You don't want to use ==, you need to use bitwise operators:

if( flags & NSShiftKeyMask ){

...

if( flags & NSAlphaShiftKeyMask ){
like image 196
Wevah Avatar answered Dec 31 '22 14:12

Wevah