I’d like to start an action (enabling autocomplete) when user types ‘@’. I have jQuery available.
Usually on a QWERTY keyboard, it is like this:
$('textarea').live('keydown', function(e) {
if (e.which === 50) {
console.log('@ has been entered.');
}
});
However it does not work correctly on an AZERTY keyboard. The keyCode = 50 corresponds to the é~/2 key. To type ‘@’ in AZERTY keyboard, it is AltGr + à@/0 key.
Edit: Autocomplete starts when @ is entered, and only after that. Example, when someone enters “Hello @” then it starts, however when he types “Hello @nothing else” the complete won’t do anything. Example: http://mrkipling.github.com/jQuery-at-username/ (it works only on QWERTY keyboard).
Use
keypressinstead ofkeydown. Whilekeydownrelates to every press of a key,keypressrelates to the translated characters, so for exampleacan be different toawhile the shift key is pressed, composed characters work, dead-keys work, and other differences in keyboard mappings are handled.