FireFox SDK keydown / keyup events

前端 未结 1 768
Happy的楠姐
Happy的楠姐 2021-01-22 18:39

Is it possible to track keydown/keyup events in FireFox addons? I would like to implement something similar as following scenario:

  • After pressing and holding the m
1条回答
  •  深忆病人
    2021-01-22 18:59

    You would need to register your event listener in each browser window for that. The high-level SDK API don't give you direct access to the browser window however, you will have to use the low-level modules for that, in particular the (currently undocumented) sdk/keyboard/observer module. It allows you to listen to key events in all browser windows, so something like this should work:

    // Register key event handlers in each browser window
    var {observer} = require("sdk/keyboard/observer");
    
    observer.on("keydown", function(event) {
      // Ignore events that have been handled elsewhere (e.g. by the web page)
      if (event.defaultPrevented)
        return;
    
      if (...)
        panel.show();
    });
    observer.on("keyup", function(event) {
      // Ignore events that have been handled elsewhere (e.g. by the web page)
      if (event.defaultPrevented)
        return;
    
      if (...)
        panel.hide();
    });
    

    Notes:

    • sdk/keyboard/observer module is completely undocumented, it might change or go away completely any time.
    • Originally the solution proposed here used WindowTracker from the sdk/window-utils module which is now deprecated. If you really want to look at browser windows yourself you would now use the (also undocumented) sdk/windows/observer module which allows listening to open and close events. The function isBrowser() to distinguish browser windows is now available via sdk/window/utils module. You would also need to use windows() function to consider already open windows, the windows observer doesn't do that automatically.

    0 讨论(0)
提交回复
热议问题