检查空格键是否被按下,鼠标是否同时使用jQuery移动? [英] Check if the spacebar is being pressed and the mouse is moving at the same time with jQuery?

查看:244
本文介绍了检查空格键是否被按下,鼠标是否同时使用jQuery移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查空格键是否同时跟踪鼠标的移动方向以及移动距离等等。

Is there a way to check if the space bar and at the same time track what direction the mouse is moving and how far etc.

这点是我想复制当你按住空格键,鼠标左键并移动鼠标时Photoshop滚动的方式,但不必按住鼠标左键。

Point of this is that I want to replicate how Photoshop scrolls when you hold the space bar, left mouse button and you move the mouse, but without having to hold down the left mouse button.

推荐答案

您可以使用 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.

这篇关于检查空格键是否被按下,鼠标是否同时使用jQuery移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆