chrome extension: how to get key events


Is there any way to get key events in a google chrome extension file - background.html - ?

document.onkeydown = function() {

Previous code doesn't work.

I assume you want to implement hotkeys for your extension. Your code should in fact work, except it works on the background page, which is usually not open to catch key presses.

To catch keypresses globally, or at least on web pages, you will have to use a content script that sends messages to the background page. The content script is injected to the open web page and insert methods for catching keypresses, and then send a message to the background page with information on which keys are pressed.