简体   繁体   中英

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. 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.

I know I can do this using the Windows API so maybe my only option is to create a plugin?

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).

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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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