在javascript中可靠的箭头键输入

时间:2018-03-29 23:57:09

标签: javascript input arrow-keys

我需要在javascript中检测箭头键输入。目前我这样做:

document.onkeydown = function(event) {
    if (event.which == 37) {
        ...
    } else if (event.which == 39) {
        ...
    } else if (event.which == 38) {
        ...
    } else if (event.which == 40) {
        ...
    }
};

document.onkeyup = function(event) {
    if (event.which == 37) {
        ... 
    } else if (event.which == 39) {
        ...
    } else if (event.which == 38) {
        ...
    } else if (event.which == 40) {
        ...
    } 
};

这样可以正常工作,但是当我按下命令+向右并首先释放箭头键时,我得到箭头键的向下事件,但只有在释放命令键时才会获得向上事件。无论我按哪个箭头键,键码都是91。我该如何处理这种情况?

我在使用谷歌浏览器的Mac上。

0 个答案:

没有答案