我想在现有代码中添加一个事件侦听器,该事件侦听器对单个键组合(alt + 1)作出反应,并向我提供多个键(例如1,2,1)作为输出,理想情况下,它们之间的延迟为1ms单个输出。如何以及从哪里开始?
我过去写过一个事件监听器,它会延迟激活一段代码,但是不知道如何使它适用于多个键代码...
function test(){
setTimeout(function(){ example(); }, 1);
}
document.addEventListener('keydown', function(e) {
if (e.keyCode == 113) {
console.log("f2 success");
test();
}
}, false);
答案 0 :(得分:0)
document.addEventListener('keydown', function(e) {
console.log( 'Button «' + e.keyCode + '» was pressed' );
if( e.altKey && ( e.keyCode == 49 || e.keyCode == 97 ) ){ // [ALT + 1]
document.dispatchEvent( new KeyboardEvent( "keydown", { bubbles: true, keyCode: 49 }) ); // [1]
document.dispatchEvent( new KeyboardEvent( "keydown", { bubbles: true, keyCode: 49 }) ); // [1]
document.dispatchEvent( new KeyboardEvent( "keydown", { bubbles: true, keyCode: 51 }) ); // [3]
}
});