-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need virtual key codes #377
Comments
Yes! We are facing the same problem in mui/mui-x#14220 (comment) to detect the copy-paste shortcuts. Only The problem is that this is deprecated in https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode: So the current state of things feels broken. |
First of all, In my experience at implementing the shortcut key handling rules of Firefox, completely compatible behavior between browsers and/or platforms may be impossible. Especially when Additionally, some keyboard layout users use 2 keyboard layouts, one is ASCII capable and the other is not so. Then, even if the ASCII capable keyboard layout is not selected, the user wants to work shortcut keys as the ASCII capable keyboard layout is selected. So, this additional information requires to store in Similarly, some keyboard layout may switch ASCII character mapping when it works with some modifiers. E.g., a keyboard layout may be Dvorak layout, but QWERTY if Finally, using punctuation with |
Actually, That's why in my "Recommended Workaround" section I recommended that shortcuts that use
I don't understand this paragraph. On a US QWERTY keyboard,
What approach?
If you accept this constraint, |
The Problem
So in this new world (since ~2020), we're supposed to use
.key
(printable character) and.code
(scancode) instead of.keyCode
/.charCode
/.which
. This has a big problem:.code === "KeyA"
because on an AZERTY (eg French) keyboard, that key is labeledQ
, and my shortcut is for Select All not Select Qll.key === "a"
because on eg a Russian keyboard, theA
key will have.key === "ф"
.key === "å"
for US English on Macs (in every browser), which is annoyingThe closest to what I want is actually sad, old, deprecated
.keyCode === 65
! Because.keyCode
was a kind of cross-platform version of OS virtual key codes (and was cross-browser compatible at least for alphanumeric keys).Severity
It seems obvious to me that this is affects almost every webapp with keyboard shortcuts. Indeed, this seems like a much more common use case than games that want physical WASD controls. Even this obscure repo has 3 open tickets on it: #229, #247, #267
Non-Solutions
In #229, #247, and #267, @garykac offers
Keyboard.getLayoutMap()
as a solution, which shipped to GA in Chrome in 2018. It has since been rejected both by Mozilla and by WebKit for privacy reasons, but to be clear it also doesn't solve the problem in the first place:keyboardLayoutMap.get("KeyA")
is specced to returnф
(altho this doesn't work for me in Chrome on Mac; instead it has an unreliable return value, see link for details).keyCode === 65
, same asA
on US English QWERTY.code === "Digit2"
, but with French AZERTY,keyboardLayoutMap.get("Digit2") === "é"
, not2
.keyCode === 50
, same as2
on US English QWERTYProposed Solution
Rather than the hardware scancode that
.code
is based on,.keyCode
is supposed to be based on the OS "virtual key code". In my testing, for alphanumeric keys, it's pretty cross-compatible, however for punctuation/symbols it's pretty inconsistent across browsers and reputedly platforms. Can we standardize a cross-platform "virtual key code" using theKeyA
etc names that respects, rather than ignores, the keyboard layout (QWERTY vs AZERTY vs Dvorak), but doesn't map to character input (ф
)?To me, unifying 2 or 3 OS virtual key code tables sounds much simpler than mucking with keyboard layout priority lists.
Open Questions
Punctuation/symbols are inconsistent because it's pretty unclear what they should do. For example, instead of a
/?
key, French AZERTY has a:/
key. Should that map to ANSI QWERTY/?
or;:
? Or some other universal-ish "reference keyboard" (context, original)?Recommended Workaround
In the meantime, the situation for web devs appears to be:
.code
and ignore.key
, because of the Ctrl-Alt-A.key === "å"
problem.code
, instead first looking at.keyCode
, which for alphanumeric keys should sensibly map back to standard ANSI QWERTY; and if.keyCode
indicates non-alphanumeric, then look at.key
..key
should be the same as.code
; and otherwise for punctuation/symbols, in general there may not be a sensible mapping to an ANSI QWERTY/reference layout, but at least it'll tell you the actual punctuation typed, which hopefully is marked on the physical keycaps or understood by the end-user.The text was updated successfully, but these errors were encountered: