I would like to let the user press a combination of keys and display the input in a readable way.
If the the pressed for example altP I would like to display alt P
.
我知道如何处理修饰键本身,但我找不到“普通”键的解决方案。我尝试使用event.code
在这种情况下给我的财产KeyP
(The event.key
or event.which
属性不合适,因为修饰键会将值更改为例如π
).
但我必须映射的值event.code
to P
,特别是对于具有特殊字符的键,因为它们在不同的布局上可能具有非常不同的值。
是否有一个属性可以检索键的显示值(不带修饰符的物理键盘上打印的值!)或一个库来映射event.code
最常见键盘布局的显示值?
(例如event.code = "Quote"
映射到Quote
/"
适合美式布局并Ä
适用于德国布局)
window.onload = function () {
document.addEventListener("keydown", function(){
document.getElementById("keydownResult").innerText = event.code + " pressed. Value is " + event.key + ".";
// Procduces "KeyP pressed. Value is π." on macOS with german layout.
});
};
<p id="keydownResult"></p>
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)