最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript keycode clash: "right arrow" and "single quote" - Stack Overflow

programmeradmin1浏览0评论

The following script does what it should, that is, it reacts on the keys "arrow left" and "arrow right". However, due to a keycode clash, it reacts on a single quote as well. It makes it impossible to enter that character into an input field. Can anything be done about that?

<script type="text/javascript">
  onload = function(){
    document.onkeypress=function(e){
      if(window.event) e=window.event;
      var keycode=(e.keyCode)?e.keyCode:e.which;

      switch(keycode){
        case 37: window.location.href='set.jsp?index=5';
          break;
        case 39: window.location.href='set.jsp?index=7';
          break;
      }
    }
  }
</script>

The following script does what it should, that is, it reacts on the keys "arrow left" and "arrow right". However, due to a keycode clash, it reacts on a single quote as well. It makes it impossible to enter that character into an input field. Can anything be done about that?

<script type="text/javascript">
  onload = function(){
    document.onkeypress=function(e){
      if(window.event) e=window.event;
      var keycode=(e.keyCode)?e.keyCode:e.which;

      switch(keycode){
        case 37: window.location.href='set.jsp?index=5';
          break;
        case 39: window.location.href='set.jsp?index=7';
          break;
      }
    }
  }
</script>
Share Improve this question asked Jan 9, 2015 at 13:30 aratajarataj 3733 silver badges12 bronze badges 4
  • For me ' is 222. I tested here: cambiaresearch./articles/15/javascript-char-codes-key-codes – Halcyon Commented Jan 9, 2015 at 13:34
  • Try keydown instead perhaps - stackoverflow./questions/20121252/… – Evan Knowles Commented Jan 9, 2015 at 13:34
  • @Halcyon For me, the test shows 222 as well. Still, this script reacts when a single quote is pressed, and an answer to stackoverflow./questions/12008215/… also uses the keycode 39 for a single quote. – arataj Commented Jan 9, 2015 at 13:43
  • keyCode and which are deprecated, you should use key. As others have said, the keypress event doesn't provide a key/keyCode/which value for keys that don't represent printable characters in all browsers, so use keypress or keyup and something like: var keycode = e.key || e.keyCode || e.which. – RobG Commented Jan 9, 2015 at 13:49
Add a ment  | 

4 Answers 4

Reset to default 3

When the user presses the single quote key, the e.keyCode property is zero, and the e.which property is 39. Executing String.fromCharCode(39) returns a single quote.

You want the keyCode if that property is in the event object:

var keycode = "keyCode" in e ? e.keyCode : e.which;

That way you get zero for the keyCode when that property exists in the event object, and when the which property also exists.

document.onkeydown = function(event) {
    event = event || window.event;

    var keyCode = "keyCode" in event ? event.keyCode : event.which;

    switch (keyCode) {
        case 37: console.log("37 was pressed", event); break;
        case 39: console.log("39 was pressed", event); break;
    }
};

Edit #1: Other menters and answers are correct. I forgot you shouldn't be detecting control keys with keypress events. Changed to onkeydown.

Full HTML example that works cross browser:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Key Codes Test</title>
</head>
<body>
    <script type="text/javascript">
        document.onkeydown = function(event) {
            event = event || window.event;

            var keyCode = "keyCode" in event ? event.keyCode : event.which;

            switch (keyCode) {
                case 37: console.log("37 was pressed", event); break;
                case 39: console.log("39 was pressed", event); break;
            }
        };
    </script>
    <input type="text" size="30">
</body>
</html>

keypress should not capture control keys like left/right arrow. if you use keydown event, single quote keycode is 222 definitely no conflict

As it is a text input, it seems you'd also have a problem when someone is trying to use the arrow keys to move the cursor within the input. Thus, stopping event propagation/bubbling should be used, and can solve the main issue you're asking about.

// assuming you've grabbed an input in var input_ele
input_ele.onkeypress = function (e) {
    e = e || window.event;

    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
};

Using this will stop the keypress event from leaving the input element, thereby never reaching the document element to trigger the unwanted behavior. In other words, you don't break the expected behavior of a very standard control element.

Use keydown instread of keypress

jS:

document.onkeydown=function(event){
  if(window.event) event=window.event;
  var keycode=(event.keyCode)?event.keyCode:event.which;
  switch(keycode){
    case 37: alert("an arrow");
      break;
    case 39: alert("another arrow");
      break;
  }
}

Fiddle : http://jsfiddle/p9x1Lj4u/2/

发布评论

评论列表(0)

  1. 暂无评论