// Written by James Bigler (jamesbigler at gmail dot com)
// Oct. 20, 2008
//
// Adapted from the FireGestures code for simulated key press.
//
// I captured the events that went into the CTRL-TAB key cycle that
// worked with TabMixPlus, then mirrored them here.

function biglerGenKey(type, ctrl, code)
{
  var evt = document.createEvent("KeyEvents");
  evt.initKeyEvent(
    type,
    true,
    true,
    null,
    ctrl,   // holds Ctrl key
    false,  // holds Alt key
    false,  // holds Shift key
    false,  // holds Meta key
    code,  // presses a special key, @see http://mxr.mozilla.org/mozilla/source/dom/public/idl/events/nsIDOMKeyEvent.idl
    0  // presses a normal key, e.g. "A".charCodeAt(0),
  );
  document.documentElement.dispatchEvent(evt);
}

// #1 CTRL-down
biglerGenKey("keydown",  true,  Ci.nsIDOMKeyEvent.DOM_VK_CONTROL);
// #2 TAB-down
biglerGenKey("keydown",  true,  Ci.nsIDOMKeyEvent.DOM_VK_TAB);
// #3 TAB-pressed
biglerGenKey("keypress", true,  Ci.nsIDOMKeyEvent.DOM_VK_TAB);
// #4 TAB-up
biglerGenKey("keyup",    true,  Ci.nsIDOMKeyEvent.DOM_VK_TAB);
// #5 CTRL-up
biglerGenKey("keyup",    false, Ci.nsIDOMKeyEvent.DOM_VK_CONTROL);

