So here's a simple solution that will catch all characters, backspace, and delete. That might work perfectly, but it seems like overkill. Then there's that very long answer from asdf. However, what we DO have with the keydown or keyup event is the actual key that was pressed ( event.key).Īs far as I can tell, any key with a length of 1 is a character (number or letter) regardless of which language keyboard you're using. Using keyDown (as in Niva's and Tarl's answers) is a bit better, but the solution is flawed because it attempts to use event.ke圜ode with omCharCode() (ke圜ode and charCode are not the same!). Using keyPress with event.which is unreliable because you can't catch a backspace or a delete (as mentioned by Tarl). They've all got some kind of flaw to them. I'm not totally satisfied with the other answers given. The approach is the same, but beware of keycode issues, see this article in quirksmode. space would not) $("input").keyup(function(event) ![]() To match the keycode with a word character (eg., a would match. The following was the original answer, but is not correct and may not work reliably in all situations. Reliable: different keyboard layouts will have different mappings. These events and the character they represent. Looks like there is a correlation between the ke圜ode property of On standard US/UK keyboards in their standard layouts, it Keyup and keydown give you information about the physical key that My view the definitive guide on this, seeĪlert("Character was typed. There are some edge cases that you should be aware of. The following example will work all the time in most browsers but ![]() ![]() Something about the character that was typed, you have to use the You can't do this reliably with the keyup event. To have a reliable solution, see Tim Down's answer (copy pasting that here as this answer is still getting views and upvotes): Note: In hindsight this was a quick and dirty answer, and may not work in all situations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |