Issues with keyup, keydown and keypress events on mobile devices
P粉461599845
2023-08-22 23:26:57
<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>
You can try
event.key,event.keyCode,event.which.If none of the above works, you can try
event.target.value.splice(-1)keydownshould work, but you can use theinputevent, 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.whichTested Android Chrome:
Use the
inputevent (e.whichis always0, 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 ) }); });