[英]How can I listen for two key press event at a same time in Javascript?
Below code works perfectly fine but I'm trying to achieve here is that I want to listen for ctrl + b
key and Output some value according to it.下面的代码工作得很好,但我想在这里实现的是我想听ctrl + b
键和 Output 根据它的一些值。
document.addEventListener( 'keydown', (event) => {
event.preventDefault();
if (event.code === 'ControlLeft') {
console.log('CTRL Left Pressed')
}
});
I've tried &&
operator in below example but it's not working for me.我在下面的示例中尝试了&&
运算符,但它不适合我。
document.addEventListener( 'keydown', (event) => {
event.preventDefault();
if (event.code === 'ControlLeft' && event.code === 'KeyB') {
console.log('CTRL Left and b Pressed')
}
});
Check out this link;查看此链接;
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/ctrlKey https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/ctrlKey
The KeyboardEvent comes with a ctrlKey
property especially for these kinds of uses. KeyboardEvent 带有一个ctrlKey
属性,特别适用于这些用途。
Your code would then become;您的代码将变为;
document.addEventListener( 'keydown', (event) => {
event.preventDefault();
if ( event.code === 'KeyB' && event.ctrlKey === true ) {
console.log('CTRL Left and b Pressed')
}
});
document.addEventListener( 'keydown', (event) => { event.preventDefault(); if( true === event.ctrlKey && 'b' === event.key ) console.log('CTRL+B Combination true;') });
Press CTRL+B Combination.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.