且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查空格键是否被按下,鼠标是否同时使用jQuery移动?

更新时间:2023-12-02 22:22:22

您可以使用 keydown() keyup() 跟踪空格键是否被按下,并在 mousemove中查看该状态( ) 事件处理程序。例如:

You can use keydown() and keyup() to track if the space bar is pressed or not and look at that state in your mousemove() event handler. For example:

var space = false;
$(function() {
  $(document).keyup(function(evt) {
    if (evt.keyCode == 32) {
      space = false;
    }
  }).keydown(function(evt) {
    if (evt.keyCode == 32) {
      space = true;
      console.log('space')
    }
  });
});

然后你的 mousemove()处理程序可以看它是否被按下。

And then your mousemove() handler can see if it's pressed or not.