4 ms·
Note that you should avoid using this (or anything else with JS) to emulate shortcuts. The reason is because what this usually looks like "oh nice I can define
by pyalot2 13y ago
Note that you should avoid using this (or anything else with JS) to emulate shortcuts.
The reason is because what this usually looks like "oh nice I can define shortcuts people can press". What it actually means is: "Ohcrap, that shortcut only works for US-US 104 keyboard layouts, and about 80% of people can't press that shortcut".
For example: suppose you want a shortcut for shift+/, which is for the US keyboard under the small right finger. But that shortcut can't be defined for SG (swiss-german) because there the / is on the numeric bar where 7 is. So if you assume (for whatever reason) that left+right small finger is a convenient shortcut, and left small finger + right index finger top isn't, then you're totally screwed. Instead, to reach the right small finger low on swiss german, you'd have to define shift + "-". Except that if you define that, it'll be one of the most uncomfortable keyboard shortcuts imaginable for US users, which find - on the numeric row top --> bottom left small finger + top right small finger = ugh.
The truth is that you cannot do reasonable shortcut definition the way that everybody would like to with JS... yet.
But wait, can't you like, define some reasonable presets and let people choose their own? The answer is no, because you don't have a reliable way to translate a keydown/keyup to a unicode char, and the unicode char is related to the modifier keys, not the key cap, so though you can do that shortcut definition, you can't show to the user what that shortcut definition actually is, because it'd be either undefined or completely wrong.
I've written at length about this here: http://codeflow.org/entries/2013/jan/30/keyboard-events-in-javascript-are-broken/ http://codeflow.org/entries/2013/jan/30/keyboard-events-in-j...
- jakub_g 13y agoOne small nitpick to your article: in Poland, QWERTY is the standard computer keyboard layout. QWERTZ used to be one in the old typewriters, but I haven't seen a single comp user with it.
- ville 13y ago> But wait, can't you like, define some reasonable presets and let people > choose their own? The answer is no, because you don't have a reliable way to > translate a keydown/keyup to a unicode char, and the unicode char is related > to the modifier keys, not the key cap, so though you can do that shortcut > definition, you can't show to the user what that shortcut definition actually > is, because it'd be either undefined or completely wrong. But couldn't you let the user write the shortcut in a text field, capture the keycodes from the keydown/keyup events and the char from the text field? Edit: This obviously doesn't work for shortcuts that do not produce any text, such as F-keys. But couldn't you at least get normal keys right most of the time?
- Groxx 13y agoThat's kind of what I was thinking. Add to that a 20-questions thing to check which keyboard layout they're using, and you can probably uniquely detect almost every keyboard layout (minus custom, of course) in a couple keypresses. But if you're going this far, you're probably significantly better off just letting people remap their shortcuts if it doesn't match what 95% of your users are using. The ones that care will love you for it, the ones that don't probably wouldn't have used it or been interested in 20-questions anyway.
- jfaucett 13y ago"The truth is that you cannot do reasonable shortcut definition the way that everybody would like to with JS... yet." couldn't agree more (learned the hard way), doing shortcuts with js is like translating your app into 50 different languages but only for half the keys - it creates a jumbled mess - best practice here is to never even do it.
- joliss 13y agoHow about single-letter shortcuts, like GitHub does? Press the ? key on GitHub to get the shortcut list. Some might not be as comfortable (e.g. / on a Swiss-German keyboard, as you say), but they'll still mostly work.
- jgalt212 13y agopyalot's points are relevant for the consumer web, but for software as a service world I'd say not as much because you know more about your customers and can either guess their keyboard or simply ask them.