Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How do I listen for general keyboard input?

I'm building a site that after the page is loaded, needs to listen for a particular keyboard string.

The event I am interested in is actually a scanner scanning an object, but it presents to the site as keyboard input, of the form ~XXX~.

I see jQuery has a keypress() event that you can bind to a particular object.

But how can I listen for general keyboard input, after $(document).ready?

like image 783
AP257 Avatar asked Dec 15 '10 21:12

AP257


1 Answers

Try this:

$(function() {
   $(window).keypress(function(e) {
       var key = e.which;
       //do stuff with "key" here...
   });
});

See it in action on jsFiddle

like image 193
Jacob Relkin Avatar answered Sep 30 '22 20:09

Jacob Relkin