简体   繁体   English

使用箭头键和jQuery移动项目

[英]Move item with arrow keys and jQuery

I have an item that can be dragged within a shape. 我有一个可以在形状内拖动的项目。 I had been also able to move the item with the arrow keys on the keyboard, using: 我还能够使用键盘上的箭头键移动项目:

$(document).bind('keypress', function(event) {
  if(event.which === 63232){ // up arrow key
    if(!event.shiftKey) howMuch = -1;   
    else if(event.shiftKey) howMuch = -10;   
    moveText(howMuch);   
  }
});

But, at least in FF, this doesn't work anymore. 但是,至少在FF中,这不再起作用了。 I alerted out the event that is happening on an arrow press and all four return zero. 我警告了按下箭头时发生的事件,所有四个都归零。

How can I detect an arrow key press? 如何检测箭头键按下? Oh yeah...javascript or jQuery. 哦,是的... javascript或jQuery。

Thank you for your time, Todd 谢谢您的时间,托德

The keycode you're using is wrong, the up arrow is 38: 您使用的键码有误,向上箭头为38:

$(document).bind('keypress', function(event) {
    if (event.which === 38) {
        moveText(event.shiftKey ? -10 : -1);   
    }
});

Arrow keycodes for reference: 箭头键代码供参考:

  • Left: 37 左:37
  • Up: 38 上:38
  • Right: 39 右:39
  • Down: 40 下:40

To find other keycodes check the example for keyPress() in the API 要查找其他键码,请在API中查看keyPress()的示例

Isn't it just: 不只是:

$(document).on('keypress', function(e) { // Note I used .on()
    if (e.keyCode == 38) {                // I never use .which
        howMuch = (e.shiftKey) ? -10 : -1;
    }
    moveText(howMuch); // never seen this function before, isn't that it?
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM