To test things further, I've created a custom keyboard layout (attached), with the following mappings:
+----------+--------------+--------------------+----------+
| Key code | Physical key | No modifiers layer | ⌘ layer |
+----------+--------------+--------------------+----------+
| 7 | x | e | c |
+----------+--------------+--------------------+----------+
| 8 | c | a | b |
+----------+--------------+--------------------+----------+
| 9 | v | c | d |
+----------+--------------+--------------------+----------+
Testing with an NSButton with .keyEquivalent = @"c" and .keyEquivalentModifierMask = NSEventModifierFlagCommand, I'm observing:
Pressing the c key alone, or with ⌘, does not trigger the button, so the logic in NSButton does not seem to be based on the key code of the event (8)
Pressing the x key alone does not trigger the button, but pressing ⌘x does, so the logic does seems to match on NSEvent.characters
Pressing the v key alone does not trigger the button, nor does pressing ⌘v, so the logic does not to match on NSEvent.charactersIgnoringModifiers
The behavior observed for [NSEvent _matchesKeyEquivalent:modifierMask:] seems to be exactly the same.
This would indicate that a "correct and robust" implementation is:
- (BOOL)performKeyEquivalent:(NSEvent *)event
{
if (event.modifierFlags & self.keyEquivalentModifierMask
&& [event.characters isEqualToString:self.keyEquivalent])
return YES;
return NO;
}
It this the case? Are the more things to consider? Thanks!
test-keyboard.keylayout.txt
Topic:
UI Frameworks
SubTopic:
AppKit
Tags: