search

Home  >  Q&A  >  body text

How String.fromCharCode function is used in html input fields with mobile keyboard

<p>I want to capture the keyboard key codes and convert them into characters. I am using String.fromCharCode javascript function but it only works with computer keyboard and not with mobile keyboard. Any help would be greatly appreciated. </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粉896751037479 days ago753

reply all(1)I'll reply

  • 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... " />

    This works as expected on my iOS Safari 15. Type a in the input and you will see the warning printed:

    ev.data: a

    reply
    0
  • Cancelreply