我正在尝试创建一个功能来检查是否要同时释放两个键以触发一个功能。这是我的代码,为了实现这个目标,我一直在摆弄它。
/* cn is used to stop the check after the two keys are
pressed once at the same time. I am not really using this
at the moment, but I will.
*/
var cn = true;
var pressed = {};
function try_buttons(event) {
if(cn === true) {
pressed[event.key] = true;
if(pressed["Shift"] && event.key === "p") {
console.log("worked");
}
}
}
它没有达到预期的目的,而是在至少按一次 shift 之后运行,然后在任何其他时间之后,只需按“p”即可运行该功能。