我自己有两个选择 keypress
和 keydown
。我倾向于使用 keypress
因为它记录了一个事件,即使键被重复,即键被长按。但问题是 keypress
只在 Firefox 中记录方向键等特殊键。如果我想跨浏览器执行此操作,则必须使用 keydown
但是长按将被记录为单个事件。
感谢任何帮助。
最佳答案
var pressed = null;
$(element).on('keydown', function(event) {
pressed = +new Date();
// do whatever else you need upon key down
});
$(element).on('keyup', function(event) {
var duration = +new Date() - pressed;
pressed = null;
// do whatever you need to do based on the duration of the press
});
我将它作为练习留给您,以解决对多个并发按下的键的支持。
关于javascript - 如何识别 jQuery 中箭头键的长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10776538/