Cmd+; keystroke with French AZERTY also produces an Esc KeyPress

Hello! I have a big complex application that struggles from a problem when a user presses Cmd+; shortcut on French AZERTY kb layout (the same physical keys as for Cmd+, on the default ABC layout), AppKit generates the following events:

  1. FlagsChanged for pressed Cmd modifier
  2. KeyDown with keycode=43 (comma), chars=";"
  3. KeyDown with keycode=53 (Esc)
  4. KeyUp with keycode=43 (comma), chars=";"
  5. FlagsChanged for released Cmd modifier

I discovered that the Esc event comes from the internal method -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] which has a snippet of logic like the following:

/* ... */

if ([event _matchesKeyEquivalent: [NSKeyboardShortcut localizedKeyEquivalentForInput:@"."] modifierMask:NSCommandKeyMask]) {
    /* explicitly send an Esc event instead */
}

/* ... */

That looks unexpected, so my first question is why does such a logic exist there?

Anyway, if there is such a logic in AppKit then I should observe the same behaviour in other AppKit apps, right? But a very simple application (which is essentially just an NSView which logs all keyboard events) doesn't receive an additional Esc event for the same keystroke on the same layout. The difference is that the internal AppKit call [NSKeyboardShortcut localizedKeyEquivalentForInput:@"."] returns @";" in case of my complex app but @"." in case of the simple one. And now I'm stuck trying to understand that difference, i.e. what could affect the behaviour of the [NSKeyboardShortcut localizedKeyEquivalentForInput:@"."] call since it's an internal AppKit call and there is no documentation for it and I don't know it's real semantic. Could anybody please advise smth?

Thus, I would like to raise the following questions:

  1. What is real purpose of the AppKit logic expressed by the code snippet above?
  2. What can affect the behaviour of the [NSKeyboardShortcut localizedKeyEquivalentForInput:] call? What is its semantic?

macOS SDK 12.3, if it matters.

Accepted Answer

Hi,

The localization of keyboard shortcuts from US QWERTY to French AZERTY was made in macOS Monterey according to a set of rules.

First, let’s focus on the lower right area:

, ? + = are kept unchanged to respect decades of habits for preferences, help, and zoom.
< and > are kept on the ⇧ layer, then localized to . and /.
< and > as well as . and / are kept in spatial order: left and right respectively. Then localized to ; and :.

This gives you the current localization.

 

Second, ⌘. on macOS has been equivalent for decades to esc, and more generally to dismissing (e.g. sheets, popovers) or stopping a task. This has been the case on iPadOS as well.
This behavior has been kept in all keyboards including French AZERTY: ⌘; sends a cancelation event because ⌘. is localized as ⌘;.

Cmd&#43;; keystroke with French AZERTY also produces an Esc KeyPress
 
 
Q