无法设置ctrl + keydown事件侦听器(未检测到)

时间:2016-03-12 23:58:25

标签: javascript jquery html

我希望有一个事件监听器,控制台在同时按下ctrl +左/右箭头键后记录文本字符串。但是,该事件未被以下功能选中:

public ICollection<int> method()
{
    ICollection<int> col = new List<int>();
    return col;
}

HashSet<int> result = (HashSet<int>) method();

知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

if分支存在问题,同时CTRL+Left按键同时记录ctrl-leftnothing detected

此外,需要停止浏览器的默认事件处理以避免问题。

这对我在Firefox 44.0.2上有用:

$(document).keydown(function (event) {
    event.preventDefault();
    if (event.which === 37 && event.ctrlKey) {
      console.log('ctrl-left');
    } else if (event.which === 39 && event.ctrlKey) {
      console.log('ctrl-right');
    } else {
      console.log('nothing detected');
    }
});