Advertisement Jump to content
Sign in to follow this  

key presses in ECMAscript

This topic is 4102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I've got code like this
<input type="text" onkeypress="return foo(this,event)">

The function foo() filters keypresses and returns true only when the value of the form element is a legal string. Some problems in the body of foo(obj,e){... obj.value does not include the effect of the new keypress. I can concatenate the new character using String.fromCharCode(key) but now I have to deal with backspace, DEL, and their ilk manually (must return true only if the NEW string is legal). Correctly handling the cursor position is more hassle. How do I avoid this work? onchange won't do because on some browsers (e.g. Opera) it doesn't actually fire on all changes (and I don't think it will let me turn back the clock and undo the change) Validating the string against a whitelist of typeable characters makes sense, but I don't want to limit myself to ASCII characters: Russian, Japanese, etc. should be allowed. How do I determine if a key press is an non-English character or a special key code (e.g. page-up) without doing exhaustive case-by-case checks. Esentially what I'm looking for is a unicode-respectful built-in keyCodeIsPrintableCharacter function.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!