Reputation: 24384
How to capture all keyboard events on a web page using HTML5? How to check whether ALT or CTRL key is pressed?
Upvotes: 1
Views: 695
Reputation: 2415
There are a few libraries focusing on capturing global key events, including short-cuts to identify metakeys like control/command/shift. You can pick a library or learn from them:
Upvotes: 0
Reputation: 990
You can use the jQuery keypress function to capture the keypress.
Example to detect Ctrl-S:
$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
alert("Ctrl-S pressed");
event.preventDefault();
return false;
});
Key codes can differ between browsers, so you may need to check for more than just 115.
Upvotes: 0
Reputation: 175017
Bind keypress
to the document
. (Broad question, broad answer).
Upvotes: 1