chrome extension : How to get key events

前端 未结 2 1870
失恋的感觉
失恋的感觉 2020-12-13 14:44

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

document.onkeydown = function() {
  alert(\'test)
};
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 15:05

    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.

提交回复
热议问题