In JavaScript? That just doesn't Scan[ Code]!

by Michael S. Kaplan, published on 2007/11/02 10:01 -04:00, original URI:

Liron asked via the Contact link (long enough ago that the point about not sending me time sensitive questions is likely clearer than it had to be):

Hey Michael,

I've read a lot about the subject of keyboard layouts, being interested in the programming side along with the linguistic side, and since I'm pretty sure you have the most intimate knowledge of the subject, and since this question requires insight, I think you're the man to ask:

I want to be able to find out which physical key a user is pressing, in Javascript. The "closest" I get to scancodes is virtual keys, that I can get from keyup and keydown events, but I have no way to tell if the user is using QWERTY, AZERTY, QWERTZ, Dvorak, or who knows what.

I actually only need "sane" options that 99% of the people use, so I'd settle for QWERTY, QWERTZ and AZERTY (And please correct me if this assertion is wrong)...

I thought that maybe I could try to match the VK I get with the text the browser sends in response (For example, if VK_Y sends
ט then I can assume it's QWERTY because that's a character that's only mapped to the QWERTY Y key, in any mapping), but that leaves me hanging in German, for example, where I can't tell if VK_Z+"z" came from QWERTY's Y key or Z key location.

Short of asking the user to hit some specific keys before starting, is there any way you can think of to get this data? Ultimately, I want to be able to map each keystroke to its physical location (=scancode, for my purposes)...

Thank you for your time, and good day,

Unfortunately, there is no great answer here. I usually look to references like Jan Wolter's JavaScript Madness: Keyboard Events and the like, which spend a lot of time showing the differences between browsers and platforms.

As you can see from that document, there is no readily accessible way of getting at keyboard scan codes. It makes a lot more sense to go with the VK values (and as that page indicates, not even them for lots of the punctuation characters).

Or better yet, if you need a bunch of characters in a specific layout on the keyboard, then you can do the work to configure the keys for the application (let the user type to assign the keys they want to use, and then you know what they decided and can take advantage of it....

Now in some specific script hosts, more information is provided, but in general these are even less consistent than the core language, which is itself not consistent with very much!


This post brought to you by A (U+0041, a.k.a. LATIN CAPITAL LETTER A)

no comments

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day