KeyEvent(String type, { Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0, int charCode: 0, int location: 1, bool ctrlKey: false, bool altKey: false, bool shiftKey: false, bool metaKey: false, EventTarget currentTarget })

Programmatically create a new KeyEvent (and KeyboardEvent).

Source

factory KeyEvent(String type,
    {Window view,
    bool canBubble: true,
    bool cancelable: true,
    int keyCode: 0,
    int charCode: 0,
    int location: 1,
    bool ctrlKey: false,
    bool altKey: false,
    bool shiftKey: false,
    bool metaKey: false,
    EventTarget currentTarget}) {
  if (view == null) {
    view = window;
  }

  var eventObj;
  // In these two branches we create an underlying native KeyboardEvent, but
  // we set it with our specified values. Because we are doing custom setting
  // of certain values (charCode/keyCode, etc) only in this class (as opposed
  // to KeyboardEvent) and the way we set these custom values depends on the
  // type of underlying JS object, we do all the construction for the
  // underlying KeyboardEvent here.
  if (canUseDispatchEvent) {
    // Currently works in everything but Internet Explorer.
    eventObj = new Event.eventType('Event', type,
        canBubble: canBubble, cancelable: cancelable);

    JS('void', '#.keyCode = #', eventObj, keyCode);
    JS('void', '#.which = #', eventObj, keyCode);
    JS('void', '#.charCode = #', eventObj, charCode);

    JS('void', '#.location = #', eventObj, location);
    JS('void', '#.ctrlKey = #', eventObj, ctrlKey);
    JS('void', '#.altKey = #', eventObj, altKey);
    JS('void', '#.shiftKey = #', eventObj, shiftKey);
    JS('void', '#.metaKey = #', eventObj, metaKey);
  } else {
    // Currently this works on everything but Safari. Safari throws an
    // "Attempting to change access mechanism for an unconfigurable property"
    // TypeError when trying to do the Object.defineProperty hack, so we avoid
    // this branch if possible.
    // Also, if we want this branch to work in FF, we also need to modify
    // _initKeyboardEvent to also take charCode and keyCode values to
    // initialize initKeyEvent.

    eventObj = new Event.eventType('KeyboardEvent', type,
        canBubble: canBubble, cancelable: cancelable);

    // Chromium Hack
    JS(
        'void',
        "Object.defineProperty(#, 'keyCode', {"
        "  get : function() { return this.keyCodeVal; } })",
        eventObj);
    JS(
        'void',
        "Object.defineProperty(#, 'which', {"
        "  get : function() { return this.keyCodeVal; } })",
        eventObj);
    JS(
        'void',
        "Object.defineProperty(#, 'charCode', {"
        "  get : function() { return this.charCodeVal; } })",
        eventObj);

    var keyIdentifier = _convertToHexString(charCode, keyCode);
    eventObj._initKeyboardEvent(type, canBubble, cancelable, view,
        keyIdentifier, location, ctrlKey, altKey, shiftKey, metaKey);
    JS('void', '#.keyCodeVal = #', eventObj, keyCode);
    JS('void', '#.charCodeVal = #', eventObj, charCode);
  }
  // Tell dart2js that it smells like a KeyboardEvent!
  setDispatchProperty(eventObj, _keyboardEventDispatchRecord);

  var keyEvent = new KeyEvent.wrap(eventObj);
  if (keyEvent._currentTarget == null) {
    keyEvent._currentTarget = currentTarget == null ? window : currentTarget;
  }
  return keyEvent;
}