Not a teaser this time but a real world problem that needs you.
I know nothing about character encodings. I know enough to have read Joel's piece and vowed that it's something I should definitely know about but not enough to have actually made tracks to aforementioned wisdom.
Today I am reminded of that vow by the gentle sting of my ignorance biting me on the bum.
For the last couple of days I've been coding keyboard shortcuts into my application. I want users to be able to change their keyboard shortcuts and to enter them into a preferences file. All good except I don't know how to map from the event.keyCode value to the Unicode value of the character in the preference file.
See my worries in a little AJAX(aka bait) demo here: keystrokes and keycodes (Non IE browsers only)
My problem is that I really don't see what the Javascript event.keyCode property actually maps to in terms of real characters.
Could someone who knows more than me about this please put me out of my misery and tell me how I can connect event.keyCode to String.charCodeAt() to Unicode?
Webkitchen is Peter Nixey's blog and website.
Originally from the UK, Peter is now in San Francisco and CEO of Clickpass a startup working to make single-sign-on and OpenID both website and consumer friendly.


3 comments:
If you get an input of 'a' (lowercase a) from the user and respond to the input with the onKeyup event, you will receive '65' as the evt.keyCode value. '65' is the unicode value for 'A'.
However, if you get an input of 'a' and respond to the onKeypress event, you will receive '97' as the evt.which value. '97' is the unicode value for 'a'.
Check out the "Browser Test Charts" section here: "http://www.mredkj.com/tutorials/validate.html
Unfortunately I don't yet know why there is a difference in the returned values among different events.
====
Also, in the onKeypress event, you must use evt.which to return the unicode value, instead of evt.keyCode.
Here is my code for the onKeypress event which will set a textbox to '97' if the user presses the 'a' key:
function handleKeypress(evt) {
document.Form1.keyCode.value = evt.which;
}
I will post my info to my website once I get the chance. In the meantime, feel free to ask me questions about this. Thanks.
-Nick
http://www.nickfessel.com/research/unicode.html
I hope you don't mind if I copied your page. If you have a problem with that, please let me know and I will remove the page from my site. Thanks.
-Nick
I don't mind in the slightest that you copied my code, thanks very much for taking the time.
So, event.which eh? I'm annoyed with myself because I came across the documentation for that the other day somewhere on the un-navigable (but greatly appreciated) Mozilla docs.
I was rather hoping that you'd see this, cheers!