javascript - Handle a keyboard that produces only keypress instead of keydown/keyup events -
i have weird situation specialty pc keyboard produces strange codes in javascript when pressing number keys.
using site (http://unixpapa.com/js/testkey.html), found keyboard produces when pressing number 4:
keydown keycode=18 which=18 charcode=0 keydown keycode=101 (e) which=101 (e) charcode=0 keyup keycode=101 (e) which=101 (e) charcode=0 keydown keycode=98 (b) which=98 (b) charcode=0 keyup keycode=98 (b) which=98 (b) charcode=0 keyup keycode=18 which=18 charcode=0 keypress keycode=52 (4) which=52 (4) charcode=52 (4)
a regular keyboard produces this:
keydown keycode=52 (4) which=52 (4) charcode=0 keypress keycode=52 (4) which=52 (4) charcode=52 (4) keyup keycode=52 (4) which=52 (4) charcode=0
so basically, strange device holds alt key, adds 2 other characters, lets go of alt key, , fires keypress event actual key while ignoring keydown , keyup completely.
i can filter out first part event.altkey
no problem, it's rest causing issues.
the problem code written 1 thing on keydown , on keyup, , adding keypress 1 keyboard messing everything...
i doubt there's solution this, , won't support ... weird piece of hardware, in case:
any ideas on how detect if keyboard creates keypress (after filtering out other event.altkey
events before that)?
i tried having global variable contains current event code , reset both on keypress , keyup , otherwise receives current keycode, can't work...
here's code:
<input type="text" name="entry" id="entry" onkeyup="entry('up', this, event);" onkeydown="entry('down', this, event);" />
yes, keyboard events supposed passed through text field, no return
needed here. , javascript:
function entry(dir, field, evt) { // filter out num lock, alt , alt pressed if (evt.keycode == 144 || evt.keycode == 18 || evt.altkey) return false; if (dir == 'up') // keyup { if (field.value.length > 0) { if (evt.keycode == 13) // enter pressed { enteruser(); // if more entries found finduser(), select first one, otherwise empty screen } else { finduser(); // start ajax checks field content vs database } } } else // keydown { if (!field.disabled) { emptyscreen(); // if text field disabled because dialog box in front, empty screen. } } }
the trick, of course, support weird keyboard while not breaking stuff other keyboards , barcode scanners working.
chance we'll have tell customer "sorry, proper keyboard" or something, it's bugging me :-)
any thoughts on this?
thanks!
ps: have jquery on site, if there's ideas use that, i'd game, too.
Comments
Post a Comment