description

# KeyEvent constructor Null safety

KeyEvent(

1. String type,
2. {Window? view,
3. bool canBubble: true,
4. bool cancelable: true,
5. int keyCode: 0,
6. int charCode: 0,
7. int location: 1,
8. bool ctrlKey: false,
9. bool altKey: false,
10. bool shiftKey: false,
11. bool metaKey: false,
12. EventTarget? currentTarget}

)

Programmatically create a new KeyEvent (and KeyboardEvent).

## Implementation

    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;
      }

      dynamic eventObj;

      // 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;
    }