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

javascript - Click event on the keyboard: up, down, left, right - Stack Overflow

programmeradmin0浏览0评论

I need an event that gives one click right, up, down or left. I want that when a condition is met (if) he give a click on the button I choose, below is an example in which should fit:

if (gesture.left) { The click event in the left direction button keypad } Else if (gesture.right) { Click the event in the right direction button keypad }

Do not want to detect which key was pressed, only one condition that after he click button that I want.

I need an event that gives one click right, up, down or left. I want that when a condition is met (if) he give a click on the button I choose, below is an example in which should fit:

if (gesture.left) { The click event in the left direction button keypad } Else if (gesture.right) { Click the event in the right direction button keypad }

Do not want to detect which key was pressed, only one condition that after he click button that I want.

Share edited May 20, 2014 at 5:27 dertkw 7,8585 gold badges38 silver badges45 bronze badges asked May 20, 2014 at 5:21 user3655060user3655060 112 silver badges4 bronze badges 5
  • Do you want to dispatch a keyboard event? On which element? – RobG Commented May 20, 2014 at 5:32
  • I want to run direction buttons (up, down, left or right) so that a condition is accepted (if). Example: if (1 == 1) {Event selects the down key on the keyboard, thus making the page down}, took a crossbow example, but you know? – user3655060 Commented May 20, 2014 at 5:44
  • Ah.. is this the kind of questions made just to test if the reader is actually paying attention?... haha.. okay got it... So the answer is NO, no click events with the keyboard. – Adrian Salazar Commented May 20, 2014 at 5:55
  • @AdrianSalazar—put focus on a button and press enter and you'll get a click event initiated by a key press. – RobG Commented May 20, 2014 at 12:16
  • @RobG you didn't read the part of up, left, down, right, right? – Adrian Salazar Commented May 21, 2014 at 7:06
Add a ment  | 

3 Answers 3

Reset to default 5

arrow keys are only triggered by onkeydown, not onkeypress

keycodes are:

  • left = 37
  • up = 38
  • right = 39
  • down = 40

http://jsfiddle/ahmadhasankhan/qqqpf/2/

Try this code:

$(function(){
    $('html').keydown(function(e){
        if(e.keyCode == 37) { // left
          }
          else if(e.keyCode == 39) { // right
          }
        else if(e.keyCode == 38) { // up
          }
        else if(e.keyCode == 40) { // down
          }
    });
});

Demo

You can also use which instead of keyCode, Like following:

$(function(){
        $('html').keydown(function(e){
            if(e.which== 37) { // left
              }
              else if(e.which == 39) { // right
              }
            else if(e.which == 38) { // up
              }
            else if(e.which == 40) { // down
              }
        });
    });
<form>
 <input id="target" type="text" value="Hello there">
</form>

<script>
  $("#target").keydown(function(e){
   if (e.keyCode == 37) { 
     alert( "left pressed" );
     return false;
   }else  if (e.keyCode == 39) { 
     alert( "right key pressed" );
     return false;
   }else  if (e.keyCode == 38) { 
     alert( "Up key pressed" );
     return false;
   }else  if (e.keyCode == 40) { 
     alert( "Down key pressed" );
     return false;
    }
   });
</script>
发布评论

评论列表(0)

  1. 暂无评论