views:

1783

answers:

2

This is what i have now:

$("input").bind("keydown",function(e){

    var value = this.value + String.fromCharCode(e.keyCode);
}

If the e.keyCode may not be an ASCII character (alt, backspace, del, arrows, etc)... I would now need to "trim" these values from "value" somehow (preferably programmatically - not with lookup tables)

I'm using jQuery.
I must use the "keydown" event. "keyPress" doesn't activate for certain keys I need to capture (esc, del, backspace, etc...). I cannot use setTimeout to get the input's value. (setTimeout(function(){},0) is too slow.)

I'm stuck. Please help.

A: 

I'm assuming this is for a game or for a fast-responding type of application hence the use of KEYDOWN than KEYPRESS.

Edit: Dang! I stand corrected (thank you Crescent Fresh and David): JQuery (or even rather the underlying DOM hosts) do not expose the detail of the WM_KEYDOWN and of other events. Rather they pre-digest this data and, in the case of keyDown even in JQuery, we get:

Note that these properties are the UniCode values.
Note, I wasn't able to find an authorititative reference to that in JQuery docs, but many reputable examples on the net refer to these two properties.

The following code, adapted from some java (not javascript) of mine, is therefore totally wrong...

The following will give you the "interesting" parts of the keycode:

  value = e.KeyCode;
  repeatCount = value & 0xFF;
  scanCode = (value >> 16) & 0xFF;  // note we take the "extended bit" deal w/ it later.
  wasDown = ((value & 0x4000) != 0);  // indicate key was readily down (auto-repeat)
  if (scanCode > 127)
      // deal with extended
  else
      // "regular" character
mjv
hm, no go, scanCode always results in 0. And e.KeyCode should be e.keyCode (KeyCode is undefined).
David Murdoch
Hey now...Hello, wait...wha?? We talkin JScript here or something?
Crescent Fresh
Yup, Javascript. Or JScript for the IE fanboys. :-)
David Murdoch
@David M. My bad, maybe JQuery "pre-digests" these code parts for us. I'm looking into it now.
mjv
@mjv: where did you get this code? Has it ever worked for you? AFAIK no DOM implementation encodes all that info into the event object (`keydown` or not).
Crescent Fresh
@Crescent: I adapted it quickly and naively from some java code of mine, way old at that... Anyway, you are totally right: none of the DOM hosts hand-out any such raw events, even for the keyboard. I fixed my prose accordingly; still looking for an authoritative doc on the subject from JQuery.com
mjv
+1  A: 

Maybe I didn't understand the question correctly, but can you not use keyup of you want to capture both?

$("input").bind("keyup",function(e){
    var value = this.value + String.fromCharCode(e.keyCode);
}
David
I'm select this as the answer because it does work. Though it doesn't technically accomplish what I needed. Thanks!
David Murdoch