3 ms·
Not sure if I agree with this summary of my article. For most web apps with relatively few keyboard shortcuts, I would actually recommend to use keypress. keyCo
by tkainrad 5y ago
Not sure if I agree with this summary of my article. For most web apps with relatively few keyboard shortcuts, I would actually recommend to use keypress. keyCode/which/charCode work when used with keypress.
The article mentions an open-source example that does it this way and works fine on international layouts: Discourse
- greggman3 5y agoWell, maybe you should go edit MDN because it has a giant red "DEPRECATED" at the top of keypress https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event https://developer.mozilla.org/en-US/docs/Web/API/Document/ke...
- tkainrad 5y agoNever said it wasn't deprecated. It still works much better than using keydown with keycode. For most apps and layouts, it's a sufficient solution.
- runarberg 5y agoWhat works best is to use `keydown`[1] with `keyboardEvent.key`[2]. Chrome took a while to implement `keyboardEvent.key` and I’m guessing that delay hindered adoption, but for the past few years (since the 5 year old Chrome 51) it is objectively the best—and arguably the only robust—way to figure out which key the user typed. 1: https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event https://developer.mozilla.org/en-US/docs/Web/API/Element/key... 2: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEve...