I have a web page and I need to intercept all keypresses before they get dispatched to whatever element in the dom has the focus. When the user types something like ~goto:/index.html: (which in fact will come from a barcode scanner) I need to capture the key presses after the ~ to parse them once I get to the second :. I’ve managed to get something with the $(document).keypress() as follows:
$(document).ready(function() {
$(document).keypress(function(e) {
// do the processing here and
// return false when ignoring keystrokes
});
});
The problem is that I get the keypress after it has gone to the focussed element. Anyway to get it before so that I can in fact reject it?
You will probably have better luck with keydown it should fire before the event
also do this
calling preventDefault will stop normal process of the key press. Actually, you may be able to stay with keypress and just use the preventDefault to finish your interception needs.