I'm trying to create shortcut when I press ctrl and space the inside function works, but it doesn't. I'm trying with ||
and it works.
document.addEventListener("keydown", function(event) {
if ((event.keyCode == 17) && (event.keyCode == 32)) {
alert('Hello');
}
});
Help please. Thank you.
There is an event.ctrlKey flag for the event.
document.addEventListener("keydown", function(event) { if (event.ctrlKey && event.keyCode === 32) { alert('Hello'); } });
html, body { width: 100%; height: 100%; margin: 0; padding: 0; } body { display: flex; justify-content: center; align-items: center; font-size: 2em; } kbd { border: 3px #EEE outset; padding: 0.125em; }
<p>Type <kbd>Ctrl</kbd> + <kbd>Space</kbd></p>
event.keyCode
不能同时具有值 17 和 32,因此您的条件没有意义。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.