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.
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");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With