开发者

Capturing Shift key activity using JavaScript

开发者 https://www.devze.com 2022-12-20 07:13 出处:网络
I am using JavaScript and jQuery and would like to call a function when the user releases the Shif开发者_运维百科t key. The code I came up with works in all the browsers I\'ve tested so far, however t

I am using JavaScript and jQuery and would like to call a function when the user releases the Shif开发者_运维百科t 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...");
      }
    });
  }
);


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.


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消