Cartesius00
Cartesius00

Reputation: 24384

Capture key events page-wide

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

Answers (3)

Cat Chen
Cat Chen

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

F481
F481

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

Madara's Ghost
Madara's Ghost

Reputation: 175017

Bind keypress to the document. (Broad question, broad answer).

Upvotes: 1

Related Questions