Tim
Tim

Reputation: 97

Capturing Shift key activity using JavaScript

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...");
      }
    });
  }
);

Upvotes: 2

Views: 2045

Answers (3)

Sarfraz
Sarfraz

Reputation: 382696

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

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

Upvotes: 0

Tim Down
Tim Down

Reputation: 324567

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.

Upvotes: 2

Marius
Marius

Reputation: 58921

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.

Upvotes: 0

Related Questions