W3cubDocs

/Dart 2

KeyEvent constructor

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).

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

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

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-html/KeyEvent/KeyEvent.html