Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSResponder not receiving keyUp event when Cmd ⌘ key held down

I'm using a custom subclass of NSView and receiving keyboard events through the keyDown/keyUp methods, everything works fine except when the "Cmd ⌘" key is pressed, keyDown events are fired as normal but the keyUp event never comes.

In our case we use the arrow keys on their own to move an image left/right/up/down and if the user holds down "Cmd ⌘" while pressing left/right it will rotate the image instead. Since we get the keyDown event the image starts rotating but it never stops as keyUp never comes. Other modifiers don't have this issue (e.g. if shift, ctrl or alt are held down while another key is pressed we get the keyUp as expected). One option is to use a different modifier but it would be nice to keep it consistent with the PC version (Cmd is used as a substitute for when Ctrl is used on Windows, keeps it consistent with standard copy/paste commands etc).

Does anyone know why it does this? It feels like a bug but is probably just strange "correct behaviour", any ideas how to get around it (other than using an alternative modifier or using the likes of direct HID access).

Thanks.

like image 207
Jez Cooke Avatar asked May 17 '13 07:05

Jez Cooke


1 Answers

You can use flagsChanged to be notified when modifier keys are down/up, see: Technical Q&A QA1519 Detecting the Caps Lock Key which is applicable to other modifier keys. See Carbon/Frameworks/HIToolbox/Events.h for more modifier key codes.

- (void)flagsChanged:(NSEvent *)event
{
    if (event.keyCode == 0x37) { // kVK_Command
        if (event.modifierFlags & NSCommandKeyMask) {
            NSLog(@"Command key is down");            
        } else {
            NSLog(@"Command key is up");
        }
    }
}
like image 89
Guilherme Rambo Avatar answered Nov 20 '22 06:11

Guilherme Rambo