B2G/gecko/testing/peptest/peptest/extension/resource/mozmill/stdlib/EventUtils.js
/**
 * Synthesize a key event. It is targeted at whatever would be targeted by an
 * actual keypress by the user, typically the focused element.
 *
 * aKey should be either a character or a keycode starting with VK_ such as
 * VK_ENTER.
 *
 * aEvent is an object which may contain the properties:
 *   shiftKey, ctrlKey, altKey, metaKey, accessKey, type
 *
 * If the type is specified, a key event of that type is fired. Otherwise,
 * a keydown, a keypress and then a keyup event are fired in sequence.
 *
 * aWindow is optional, and defaults to the current window object.
 */
Synthesize a key event. It is targeted at whatever would be targeted by an
actual keypress by the user, typically the focused element.

aKey should be either a character or a keycode starting with VK_ such as
VK_ENTER.

aEvent is an object which may contain the properties:
shiftKey, ctrlKey, altKey, metaKey, accessKey, type

If the type is specified, a key event of that type is fired. Otherwise,
a keydown, a keypress and then a keyup event are fired in sequence.

aWindow is optional, and defaults to the current window object.
Synthesize a key event . It is targeted at whatever would be targeted by an actual keypress by the user typically the focused element . aKey should be either a character or a keycode starting with VK_ such as VK_ENTER . aEvent is an object which may contain the properties : shiftKey ctrlKey altKey metaKey accessKey type If the type is specified a key event of that type is fired . Otherwise a keydown a keypress and then a keyup event are fired in sequence . aWindow is optional and defaults to the current window object .
function synthesizeKey(aKey, aEvent, aWindow)
{
  if (!aWindow)
    aWindow = window;
  var utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
                      getInterface(Components.interfaces.nsIDOMWindowUtils);
  if (utils) {
    var keyCode = 0, charCode = 0;
    if (aKey.indexOf("VK_") == 0)
      keyCode = getKeyEvent(aWindow)["DOM_" + aKey];
    else
      charCode = aKey.charCodeAt(0);
    var modifiers = _parseModifiers(aEvent);
    if (!("type" in aEvent) || !aEvent.type) {
      var keyDownDefaultHappened =
          utils.sendKeyEvent("keydown", keyCode, charCode, modifiers);
      utils.sendKeyEvent("keypress", keyCode, charCode, modifiers,
                         !keyDownDefaultHappened);
      utils.sendKeyEvent("keyup", keyCode, charCode, modifiers);
    } else {
      utils.sendKeyEvent(aEvent.type, keyCode, charCode, modifiers);
    }
  }
}
function synthesizeKey ( aKey , aEvent , aWindow ) { if ( ! aWindow ) aWindow = window ; var utils = aWindow . QueryInterface ( Components . interfaces . nsIInterfaceRequestor ) . getInterface ( Components . interfaces . nsIDOMWindowUtils ) ; if ( utils ) { var keyCode = 0 , charCode = 0 ; if ( aKey . indexOf ( "VK_" ) == 0 ) keyCode = getKeyEvent ( aWindow ) [ "DOM_" + aKey ] ; else charCode = aKey . charCodeAt ( 0 ) ; var modifiers = _parseModifiers ( aEvent ) ; if ( ! ( "type" in aEvent ) || ! aEvent . type ) { var keyDownDefaultHappened = utils . sendKeyEvent ( "keydown" , keyCode , charCode , modifiers ) ; utils . sendKeyEvent ( "keypress" , keyCode , charCode , modifiers , ! keyDownDefaultHappened ) ; utils . sendKeyEvent ( "keyup" , keyCode , charCode , modifiers ) ; } else { utils . sendKeyEvent ( aEvent . type , keyCode , charCode , modifiers ) ; } } }
Synthesize a key event.
Synthesize a key event .
// Send keydown + keypress + keyup events. // Send standalone event.