views:

38

answers:

3

I'm building a Firefox extension and would like to allow the user to delete objects from the tree with the delete key. 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.

A: 

Use the key element to define keyboard shortcuts for the window. See tutorial here.

pc1oad1etter
If you only want the delete key to have an effect when the tree has focus (which I think is probably what users would expect), then it would be better to to use the `onkeypress` attribute, right? From your link: "Key events are only sent to the element that has the focus. ... If no element is focused, the key event will instead be targeted at the XUL document itself. In this case, you can add an event listener to the window. Normally though, if you want to respond to keys globally, you will use a keyboard shortcut as described earlier." So I *think* Josh's answer makes more sense in this case.
MatrixFrog
Probably so; I don't have enough information about his UI and intentions to tell you whether or not he intended to be a general shortcut for the window or not.
pc1oad1etter
+1  A: 

Actually, I figured it out. I added this to the tree element in the XUL:

onkeypress="deleteSelection(event);"

Here is my 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);
    }
  }
}
Josh
A: 

I just noticed this when looking at the documentation for nsITreeView:

performAction()

A command API that can be used to invoke commands on the selection. 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.

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.

MatrixFrog
Actually, it looks like this is probably not the best way to do this. https://bugzilla.mozilla.org/show_bug.cgi?id=406588
MatrixFrog