gpt4 book ai didi

javascript - 如何检测退出键的 KeyboardEvent?

转载 作者:行者123 更新时间:2023-12-01 02:52:35 25 4
gpt4 key购买 nike

许多功能是否已被弃用或者不属于 KeyboardEvent 对象类当前标准的一部分。只是想知道为什么每当我按 shiftesc 时。我的 Chrome 或 Mozilla 控制台上没有显示任何按键。

document.addEventListener("keypress", (e) => {
console.log(e.keyCode);
console.log(e.key);
console.log(e.code);
console.log(e.shiftKey);
});

使用的浏览器控制台

  • “Mozilla/5.0(Windows NT 10.0;WOW64;rv:56.0)Gecko/20100101 Firefox/56.0”
  • “Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/61.0.3163.100 Safari/537.36”
  • [网络检查器]“Mozilla/5.0(Macintosh;Intel Mac OS X 10_12_6)AppleWebKit/604.1.38(KHTML,如 Gecko)版本/11.0 Safari/604.1.38”

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

最佳答案

keypress 仅在按下可打印字符时触发。对于所有按键,您需要使用 keyupkeydown

来自MDN :

keypress

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt, Shift, Ctrl, or Meta.

document.addEventListener("keydown", (e) => {
console.log(e.keyCode);
console.log(e.key);
console.log(e.code);
console.log(e.shiftKey);
});
<h1>Click into this area and then press ESC</h1>

关于javascript - 如何检测退出键的 KeyboardEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46878707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com