views:

220

answers:

3

I am using JavaScript and jQuery and would like to call a function when the user releases the Shift key. The code I came up with works in all the browsers I've tested so far, however the following warning appears in the error console using Firefox 3.5.7. I've tried jQuery 1.2.6, 1.3.2 and 1.4.1.

Warning: The 'charCode' property of a keyup event should not be used. The value is meaningless.

I searched Google for this warning but could not find a definitive answer as to how to prevent it from happening. This is the code I'm using, does anyone have any suggestions?

$(document).ready(
  function() {
    $(document).keyup(function(e) {
      if(e.keyCode == 16) {
        alert("Do something...");
      }
    });
  }
);
A: 

the charCode might be used by jQuery (since you aren't using it). In that case you shouldn't really care about the error message.

Marius
Yes, it's a jQuery issue in copying properties for its event wrapper object. You'll notice the same warning for every keypress at StackOverflow.
bobince
+2  A: 

You could not use jQuery. This is pretty simple without it:

document.onkeyup = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 16) {
        alert("Shift");
    }
};

Note that some older browsers (Safari prior to version 3.1, Firefox prior to version 1.0) don't fire keyup events for modifier keys such as Shift.

Tim Down
A: 

You need to consider cross-browsers issues as well, see the cross browser solution here:

http://cross-browser.com/x/examples/shift_mode.php

Sarfraz