search

Home  >  Q&A  >  body text

Issues with keyup, keydown and keypress events on mobile devices

<p><br /></p> <p>I've been trying to get it to work but I don't know what's going on, I have the following code: </p> <pre class="brush:php;toolbar:false;">$('#buscar-producto').on('keydown', function(e){ console.log('hello'); console.log(e.keyCode); });</pre> <p>It works on a computer but not on a phone...</p> <p><strong>Edit:</strong> I need to get the keyCode when the key is pressed...</p>
P粉461599845P粉461599845449 days ago564

reply all(2)I'll reply

  • P粉431220279

    P粉4312202792023-08-23 22:08:13

    You can try event.key, event.keyCode, event.which.

    If none of the above works, you can try event.target.value.splice(-1)

    reply
    0
  • P粉550257856

    P粉5502578562023-08-23 19:41:36

    keydown should work, but you can use the input event, which seems to have a bad effect on Android mobile devices...
    To get the code of a pressed key, you can use jQuery's normalized Event.which

    Tested Android Chrome:

    Use the input event (e.which is always 0, so seems to be a bug on Android devices)

    jQuery(function($) { // DOM准备就绪和$别名已保证
    
      $('#buscar-producto').on('input', function(e){
        var key = e.which || this.value.substr(-1).charCodeAt(0);
        alert( key )
      });
    
    });
    <input type="text" id="buscar-producto" placeholder="搜索...">
    
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>

    reply
    0
  • Cancelreply