Chrome扩展:如何获得关键事件 [英] chrome extension : How to get key events

查看:130
本文介绍了Chrome扩展:如何获得关键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以在Google Chrome扩展文件中获取关键事件 - background.html - ?

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

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

以前的代码无效。

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.

要在全局或至少在Web上捕获按键页面,您将不得不使用内容脚本发送消息添加到背景页面。内容脚本被注入到打开的网页中,并插入用于捕获按键的方法,然后将消息发送到后台页面,并提供有关按下哪些按键的信息。

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.

这篇关于Chrome扩展:如何获得关键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆