Issues with keyup, keydown and keypress events on mobile devices
P粉461599845
P粉461599845 2023-08-22 23:26:57
0
2
576
<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粉461599845
P粉461599845

reply all(2)
P粉431220279

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

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

P粉550257856

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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template