Javascript - 无法使我的KeyDown事件正常工作

时间:2017-10-11 12:51:58

标签: javascript addeventlistener keydown

我正在尝试创建自己的香草JS灯箱。我通过KeyDown事件遇到了键盘控制问题。这是代码:

"body": "https://github.com/github/linguist/pull/3638  
attempted to include support for the `.k` extension too, 
which would have posed a problem due to clashing with non-Kdb, 
yet undefined, languages. 
This PR doesn't mention that extension at all. 
Whilst this isn't a problem, is `.q` the preferred extension for 
Q/Kdb+?\r\n\r\nI 
notice that https://github.com/KxSystems/kdb which you reference 
includes `.k` files which won't be recognised as Q/Kdb+ once 
this PR has been merged, hence the question.",

"path": "lib/linguist/languages.yml",

"diff_hunk": "@@ -3601,6 +3601,18 @@ Python traceback:\n   
  tm_scope: text.python.traceback\n   
  ace_mode: text\n   
  language_id: 304\n+Q:\n+  
  type: programming\n+  
  color: \"#0040CD\"\n+  
  aliases:\n+  -Kdb+\n+  
  extensions:\n+  - \".q\"",

第一个语句(关闭模式的esc键)正常工作。但是,所有其他按下的键(除了esc键之外)都会触发第二个函数previousSlide()。我已经尝试了各种各样的东西,例如省略了每个密钥代码而不是一个,返回所有错误,但无济于事。

如果我用好的情况替换那个代码&切换,它按预期工作:

function keyDown(e) {

    if(e.keyCode === 27) {

        closeModal();
    }
    else if(e.keyCode === 37 || 38 || 100 || 104 || 65 || 87) {

        previousSlide();

    }
    else if(e.keyCode === 39 || 40 || 102 || 98 || 68 || 83) {

        nextSlide();

    }
}

该函数由addEventListener方法触发:

function keyDown(e) {

switch (e.keyCode) {
    case 27:
    closeModal();
    break;
    case 37:
        previousSlide();
        break;
    case 38:
        previousSlide();
        break;
    case 39:
        nextSlide();
        break;
    case 40:
        nextSlide();
        break;

}

我错过了什么吗?请帮忙。感谢。

0 个答案:

没有答案