本文介绍了FireFox SDK的keydown / keyup事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能跟踪FireFox插件中的keydown / keyup事件?
我想实现类似于以下的场景:
$ b

  • 按下并按住修改键盘面板

  • 在按住这些修改键的同时,按其他键将导致面板操作

  • 释放修饰键时,面板将消失


解决方案

您需要在每个浏览器窗口中注册您的事件侦听器。高级的SDK API不能直接访问浏览器窗口,但是你必须使用底层的模块,特别是(当前没有记录的) sdk / keyboard / observer 模块。它允许你在所有浏览器窗口中监听关键事件,所以类似这样的应用程序应该可以工作:
$ b

  //在每个浏览器窗口中注册关键事件处理程序
var {observer} = require(sdk / keyboard / observer);
$ b observer.on(keydown,function(event){
//忽略其他地方已经处理的事件(例如通过网页)
if(event.defaultPrevented )
return;

if(...)
panel.show();
});
observer.on(keyup,function(event){
//忽略在别处处理的事件(例如通过网页)
if(event.defaultPrevented)
返回;

if(...)
panel.hide();
});

注意:


  • sdk / keyboard / observer 模块完全没有记录,可能随时改变或完全消失。这里建议使用来自 sdk / window-utils 模块的 WindowTracker ,现在已经不推荐使用了。如果你真的想看自己的浏览器窗口,你现在可以使用(也没有记录的) sdk / windows / observer 模块,它允许监听打开和关闭事件。用来区分浏览器窗口的函数 isBrowser()现在可以通过。您还需要使用考虑已经打开的窗口,窗口观察者不会自动执行该操作。

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 modifier key panel appears
  • While holding these modifier keys, pressing the other keys will cause some actions with the panel
  • When modifier key released, the panel disappears

解决方案

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.

这篇关于FireFox SDK的keydown / keyup事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:44