When I wanted to react to any changes to a
input textbox immediately, my first instrinct was to use
onChange, however, is called when the
input loses focus.
onKeyPress then? Isn’t called on backspaces.
onKeyDown, maybe? It does get called, but the effect of the keystroke isn’t yet applied, for the return value determines whether it that is done in the first place. (Same story for
onKeyPress by the way.)
onKeyUp does work a bit, except if someone is holding down a single key, for a while.
The solution: hook
onKeyUp and use
setTimeout with a timeout of 0. Yugh. I hate DOM.
: I actually tested this only on one browser.
When I want to generate usernames from real names, which can contain non-ascii characters, you can’t simply ignore the unicode characters. For instance, email@example.com is the right e-mail address for Daniëlle, firstname.lastname@example.org isn’t.
There’s trick. Unicode has got a single code for ë itself, but it has also got a code which (simplified) adds ¨ on top of the previous character. The unicode standard defines a normal form in which (at least) all such characters, which can be, are represented using such modifiers. If you then simply ignore the non-ascii representable codes, you’ll get the desired result.
unicodedata.normalize('NFKD', txt).encode('ASCII', 'ignore').
However, this isn’t the right solution. For instance, in german, one prefers ue as a replacement of ü over u.