Javascript JQuery-多键按下检测

Javascript JQuery-多键按下检测,javascript,jquery,arrays,keypress,Javascript,Jquery,Arrays,Keypress,我有多个按键检测问题。我对js/JQuery有点陌生,所以这可能是个愚蠢的错误。。但是我找不到它。任何帮助都将不胜感激 //========== KEY LOGGING ========== var pressedKeys = []; // == KEYDOWN == $(document.body).keydown(function(e){ pressedKeys[e.which] = true; //left if(presse

我有多个按键检测问题。我对js/JQuery有点陌生,所以这可能是个愚蠢的错误。。但是我找不到它。任何帮助都将不胜感激

    //========== KEY LOGGING ==========
  var pressedKeys = [];      
// == KEYDOWN ==
$(document.body).keydown(function(e){
    pressedKeys[e.which] = true;
        //left    
    if(pressedKeys[37] = true)
        {
        x -= speed;
        }         
        //up    
    if(pressedKeys[38] = true)
        {
        y -= speed;
        }         
        //right    
    if(pressedKeys[39] = true)
        {
        x += speed;
        }          
        //down    
    if(pressedKeys[40] = true)
        {
        y += speed;
        }          
        //+    
    if(pressedKeys[107] = true)
        {
        speed += 1;
        }           
        //-
    if(pressedKeys[109] = true)
        {
        speed -= 1;
        } 
});

// == KEYUP ==    
$(document.body).keyup(function (e) {
     pressedKeys[e.which] = false;   
});

编辑:问题是,当按下任何键时,它会激活所有方向。。我不知道为什么。

将if语句从
if(this=that)
更改为
if(this=that)
。“=”符号实际上将变量设置为true,即使在if语句中也是如此。只有“==”检查它的计算结果是否为真。

我在这里看到了一些东西:

您正在使用赋值运算符而不是相等运算符:)

所以发生的是

if (x = true) { /* code always runs */ }; 
试一试

===可以说比==更好,因为它不会进行任何自动铸造。我相信1==true将返回true,但1==true将返回false

第二件事是你确定jquery已经加载了吗?也许把整件事都包装在$(document.ready()上。请参见下面的快速示例:

  //========== KEY LOGGING ==========
var pressedKeys = [];      
// == KEYDOWN ==

$(document).ready(function(event) {

    $(document.body).keydown(function(e){
        pressedKeys[e.which] = true;
            //left    
        if(pressedKeys[37] === true)
            {
            x -= speed;
            }         
            //up    
        if(pressedKeys[38] === true)
            {
            y -= speed;
            }         
            //right    
        if(pressedKeys[39] === true)
            {
            x += speed;
            }          
            //down    
        if(pressedKeys[40] === true)
            {
            y += speed;
            }          
            //+    
        if(pressedKeys[107] === true)
            {
            speed += 1;
            }           
            //-
        if(pressedKeys[109] === true)
            {
            speed -= 1;
            } 

        alert(speed);
    });

// == KEYUP ==    
    $(document.body).keyup(function (e) {
         pressedKeys[e.which] = false;   
    });
});
最后,如果x和y没有定义,或者速度没有定义,但是没有更多的代码,那么您仍然会遇到错误。我不能确定您会遇到什么错误

好的,希望这有帮助


您可能还想查看,这样您就不会将这些x、y和速度变量保留为全局变量,而这并不总是有效的。有些浏览器使用e。keyCode@Samual对我来说,至少在我使用swicth时起作用了。问题是,当我按下任何箭头时,它会激活所有方向。这可能是因为您使用的是=(赋值)而不是==(测试)人,非常感谢!你刚刚帮我省了几个小时的时间去寻找问题。我完全忘记了这一点:真遗憾,我以前犯过很多次这样的错误。实际上,有时执行
if(this=that)
会很有用,因为它会测试是否确实发生了将“that”赋值给“this”的操作,如果以某种方式失败,则计算结果为false。无论如何,要记住一点。
  //========== KEY LOGGING ==========
var pressedKeys = [];      
// == KEYDOWN ==

$(document).ready(function(event) {

    $(document.body).keydown(function(e){
        pressedKeys[e.which] = true;
            //left    
        if(pressedKeys[37] === true)
            {
            x -= speed;
            }         
            //up    
        if(pressedKeys[38] === true)
            {
            y -= speed;
            }         
            //right    
        if(pressedKeys[39] === true)
            {
            x += speed;
            }          
            //down    
        if(pressedKeys[40] === true)
            {
            y += speed;
            }          
            //+    
        if(pressedKeys[107] === true)
            {
            speed += 1;
            }           
            //-
        if(pressedKeys[109] === true)
            {
            speed -= 1;
            } 

        alert(speed);
    });

// == KEYUP ==    
    $(document.body).keyup(function (e) {
         pressedKeys[e.which] = false;   
    });
});