13

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.

Jez Cooke
  • 131
  • 4
  • 1
    This might be a possible solution: http://stackoverflow.com/questions/4001565/missing-keyup-events-on-meaningful-key-combinations-e-g-select-till-beginning?rq=1 I am having the same issue. – Bikush Jun 20 '13 at 11:04

1 Answers1

1

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");
        }
    }
}
Andrew
  • 7,340
  • 3
  • 38
  • 47
Guilherme Rambo
  • 1,703
  • 15
  • 19
  • 4
    The OP was asking about the keyUp event for the key pressed in conjunction with the command key. This keyUp event never arrives. – Tim Kane Jul 21 '17 at 21:14