首页  >  问答  >  正文

String.fromCharCode 函数如何在 html 输入字段中用于移动键盘

<p>我想捕获键盘按键代码并将其转换为字符。我正在使用 String.fromCharCode javascript 函数,但它仅适用于计算机键盘,不适用于移动键盘。任何帮助将不胜感激。</p> <pre class="brush:php;toolbar:false;">$(".inputsmeter").keyup(function (e) { let key = e.which; let c = String.fromCharCode(key); alert(c); });</pre></p>
P粉896751037P粉896751037414 天前700

全部回复(1)我来回复

  • P粉334721359

    P粉3347213592023-09-02 00:28:01

    const inputElem = document.getElementById('input')
    inputElem.addEventListener('input', (ev) => { 
      console.log(ev);
      alert(`e.data: ${ev.data}`);
    });
    <input id="input" type="text" placeholder="type something... " />

    这在我的 iOS Safari 15 上按预期运行。 在输入中键入 a 将看到警告打印:

    ev.data: a

    回复
    0
  • 取消回复