如何防止表情符号快捷方式

How to prevent emoji shortcuts

我在阻止浏览器通过触发 'alt + 1'、'alt + 2'.[=14= 等快捷方式在 'textarea' 和 'input' 元素内创建表情符号时遇到问题]

此代码禁止与所述元素进行任何交互,但 'alt + 1' 仍会创建笑脸。在 Chrome 和 Edge 上测试。我错过了什么吗?

'use strict';
(() => {
  document.onkeydown = document.onkeypress = document.onkeyup = ev => {
    ev.stopPropagation();
    ev.preventDefault();
    return false;
  }
})();

https://jsfiddle.net/9Lr0hays/1/

注意:我不想直接与元素交互(使用选择器)。

我说的是这些符号: http://www.alt-codes.net/

相关:Disable Alt Codes/Characters with JavaScript

此问题的一种可能解决方案是检查键码。

<input type="text" id="number" onkeypress="return iskey(event);" />
<script>
function iskey(evt) {
    var theEvent = evt || window.event;
    var theVal = theEvent.target.value;
    var key = theEvent.keyCode || theEvent.which;

    key = String.fromCharCode(key);

    if (key.length == 0) return;
        if (theEvent.preventDefault) theEvent.preventDefault();
        return false;
}
</script>

在这里工作:https://jsfiddle.net/1569atLz/23/

非常严重的错误。我在 fiddle 中写了 document.keypress 而不是 document.onkeypress (片段是正确的)。

工作解决方案:

'use strict';
(() => {
  document.onkeypress = ev => false;
})();

https://jsfiddle.net/9Lr0hays/3/