Detect a key press

Is there a way to detect when a user presses a particular key in an input field? for instance If the user to enter a 10 character text, but if the press a "\" I want to trigger an event.

Thanks for your help



Hi Gonzalo,

Yes, it is possible.

You can use also the OnChange event of the input, but it fires after a "lag", what may pose a problem.
You can also use JavaScript, as you can bound the input to an event like onkeydown, onkeypress or onkeyup ( Beware that in mobile this is not "trivial", as each OS/device happens to trigger different events.


Eduardo Jauch



If someone answer your question, mark it as correct. That will help other to find answers to similar questions.


I am using a barcode scanner to enter text it in a search field, and I wanted to to trigger the search automatically after the barcode is read. Since barcode scanners add a "carriage return" character after the data, I wanted to capture that and use it. 

I used the OnChange method to identify last character entered, but it did not work with the carriage return. If I reconfigure the scanner to send another character, like a ",", my method works. Is just not that ellegant because you can see the "," after the data.