简体   繁体   English

在没有激活浏览器窗口的情况下监听按键事件

[英]Listen for keydown event without browser window active

I have a Firefox add-on that listens to keydown events when a browser window is active but I also want to receive the events when no browser window is active. 我有一个Firefox插件,当浏览器窗口处于活动状态时,它侦听按键事件,但我也想在没有浏览器窗口处于活动状态时接收事件。 I'm currently adding event listeners to all browser windows and I tried creating a Firefox hotkey but it has the same limited scope as the browser window method. 我目前正在将事件侦听器添加到所有浏览器窗口,并且尝试创建Firefox热键,但是它与浏览器窗口方法的作用域相同。

I know I can do this using the Windows API so maybe my only option is to create a plugin? 我知道我可以使用Windows API来做到这一点,所以也许我唯一的选择就是创建一个插件?

This is not directly possible. 这不是直接可能的。 The browser can not pass an event to an extension if the browser does not receive the event from the windowing system. 如果浏览器未从窗口系统收到事件,则该浏览器无法将事件传递给扩展程序。 Presumably, you could create an OS/windowing system specific plugin which allows taping into the API for the windowing system to generally receive such events (assuming this is permitted by the windowing system API). 大概您可以创建一个特定于OS /窗口系统的插件,该插件可以利用窗口系统的API来通常接收此类事件(假设窗口系统API允许这样做)。

You appear to be wanting to receive events which occur in the windowing system which are not targeted to any browser window. 您似乎想接收在窗口系统中发生的事件,这些事件并非针对任何浏览器窗口。 Windowing systems generally do not pass UI events to windows which are not active, or not being manipulated in some way. 窗口系统通常不会将UI事件传递给未激活或未通过某种方式操纵的窗口。 Thus, the browser has no information that such events have occurred. 因此,浏览器不知道发生了此类事件。

In general, this will probably violate the user's expectations as to how the browser and windowing system will operate. 通常,这可能会违反用户对浏览器和窗口系统将如何操作的期望。 You should seriously consider if it is appropriate for your add-on to do this. 您应该认真考虑附加组件是否适合这样做。 A possible reason to do so would be to have an add-on that adds a system level hotkey to bring the most recent window/tab to the front of the windowing system. 这样做的可能原因是要有一个附加程序,该附加程序会添加系统级热键,以将最新的窗口/选项卡带到窗口系统的前面。 In such a case, where it is explicitly the function of the add-on, then it is reasonable to do this. 在这种情况下,如果明确是附加组件的功能,那么这样做是合理的。 At a minimum, you may conflict with other windowing system level hotkeys which are either defaults, or have been set by the user. 至少,您可能会与其他窗口系统级热键冲突,这些热键要么是默认设置,要么是由用户设置的。 Care should be taken to, at a minimum, permit the user to change the hotkey assigned for whatever function you are creating. 至少应注意允许用户更改为您正在创建的任何功能分配的热键。

Use js-ctypes to register the hotkey via the Windows API . 使用js-ctypes 通过Windows API注册热键 You will also have to listen to the hotkey events using that API so put your code in a ChromeWorker so that it doesn't interfere with the main Firefox thread. 您还必须使用该API侦听热键事件,因此将您的代码放入ChromeWorker中,以免干扰Firefox主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM