Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to change the keyboard shortcut for showing the context menu in Firefox (the equivalent of pressing Shift-F10 on Linux or Windows or Control-Space on OS X). It seems like this shortcut is handled at a different level from normal keyboard shortcuts (it doesn't show up in the Keyconfig extension that allows remapping of most keyboard shortcuts).

I have tried creating key and mouse events to trigger the context menu (using the following code that is mapped to a shortcut with Keyconfig) but they haven't worked so far. I haven't been able to track down where in the source code Firefox handles the keyboard shortcut for the context menu or if there is a single function that I could call to show it.

Mouse event:

var focused = document.commandDispatcher.focusedElement;
if(!focused) focused = document.commandDispatcher.focusedWindow.document.activeElement;
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 2, null);
focused.dispatchEvent(evt);

Key event:

var focused = document.commandDispatcher.focusedElement;
if(!focused) focused = document.commandDispatcher.focusedWindow.document.activeElement;
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent("keypress", true, true, null, false, false, true, false, 0x79, 0);
focused.dispatchEvent(evt);
share|improve this question

2 Answers 2

i am not a expert but did you tried to find the setting's in about:config ?

if not then enter about:config in location box and hit enter and you will find lots of setting's there and do tell me if you find any setting's there or not .

share|improve this answer
    
Thanks for the suggestion. I did not find any relevant preference in about:config, but I checked on a different computer from the one I was originally using when I asked the question and discovered that I had actually made such a shortcut in Keyconfig on that computer (and I only realized this by looking in about:config). From that old shortcut, I produced my answer. –  ws_e_c421 Feb 24 at 19:18
up vote 0 down vote accepted

I found that this same question had been asked previously on MozillaZine here. That solution puts the context menu in the top left corner of the screen. I tried using the solution given to this other question about getting DOM screen positions to produce the code below which places the context menu closer to the active element, but it is still buggy (sometimes the menu is a bit off from the element):

var target = (document.commandDispatcher.focusedElement || document.commandDispatcher.focusedWindow.document.documentElement);

var screenX=0;
var screenY=0;
if ("boxObject" in target) {
  screenX=target.boxObject.x;
  screenY=target.boxObject.y;
} else {
  var box;
  try {
    box = elem.getBoundingClientRect();
  } catch(e) {}

  // Make sure we're not dealing with a disconnected DOM node
  if (box) {

    var body = document.body,
      clientTop = document.documentElement.clientTop || body.clientTop || 0,
      clientLeft = document.documentElement.clientLeft || body.clientLeft || 0,
      scrollTop = window.pageYOffset || body.scrollTop,
      scrollLeft = window.pageXOffset || body.scrollLeft,
      top = box.top + scrollTop - clientTop,
      left = box.left + scrollLeft - clientLeft;
  }

  screenX=target.offsetLeft;
  screenY=target.offsetTop;
}

var e = document.createEvent("MouseEvents");
e.initMouseEvent("contextmenu", true, false, null, 0, screenX, screenY, 0, 0, false, false, false, false, 0, null);
target.dispatchEvent(e);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.