简体   繁体   English

如何使用Delete键从XUL中的树中删除对象?

[英]How do you delete an object from a tree in XUL with the delete key?

I'm building a Firefox extension and would like to allow the user to delete objects from the tree with the delete key. 我正在构建Firefox扩展,并希望允许用户使用Delete键从树中删除对象。 I currently call a function when the user presses a button, but would like to allow them to just press the delete key on their keyboard. 我目前在用户按下按钮时调用一个函数,但我希望允许他们仅按下键盘上的Delete键。

Actually, I figured it out. 实际上,我知道了。 I added this to the tree element in the XUL: 我将其添加到XUL的tree元素中:

onkeypress="deleteSelection(event);"

Here is my Javascript: 这是我的Javascript:

function deleteSelection(event){
  if(event.keyCode == KeyEvent.DOM_VK_DELETE)
  {   
    var t = document.getElementById('gs-scrapeToolbar-middlePanel-dom-tree');
    if (t.currentIndex > -1) {
      treeView.model.splice(t.currentIndex, 1);
      treeView.treeBox.rowCountChanged(t.currentIndex, -1);
    }
  }
}

Use the key element to define keyboard shortcuts for the window. 使用key元素定义窗口的键盘快捷键。 See tutorial here . 请参阅此处的教程

I just noticed this when looking at the documentation for nsITreeView: 在查看nsITreeView的文档时,我只是注意到了这一点:

performAction()

A command API that can be used to invoke commands on the selection. 可用于在所选内容上调用命令的命令API。 The tree will automatically invoke this method when certain keys are pressed. 当按下某些键时,树将自动调用此方法。 For example, when the DEL key is pressed, performAction will be called with the delete string. 例如,当按下DEL键时,将使用删除字符串调用performAction。

void performAction(in wstring action);

So I guess that's another way you could accomplish this: 所以我想这是您可以完成此操作的另一种方法:

void performAction(action) {
  if (action == 'delete') {
    // delete the thing
  }
}

although I haven't tested it. 尽管我还没有测试过。

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

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