How to send KeyEvent (to simulate input) to Dart?

How can we generate and send a key event ("keypress", "keyup", "keydown") in Dart?

I tried:

// Context: import 'dart:html' as dom;
...
InputElement input = dom.querySelector(...);
var ev = new KeyEvent('keypress', keyCode: 65);
print("ev=$ev, and (ev is Event) is ${ev is Event}");
  // ==> output: ev=Instance of 'KeyEvent', and (ev is Event) is true
input.dispatchEvent(ev);
  // ==> yields

The last statement calls:

Caught Invalid class: expected instance of Event
#0      Node.dispatchEvent (.../dart/dart/html/Node.dart:586)

Reports of exceptions that are evnot instances Event, and yet from the printed output we see what it is.

+3
source share
1 answer

This seems to be recognized as a bug: Dart Issue # 16869, "Unable to send KeyEvent . " Once this is fixed, I will post an update.

, , , TextEvent. ,

  /**
   * This function simulates typing the given value string into the input
   * field. It has the side-effect of setting the window focus to the input.
   */
  void setInputValueViaTextEvent(InputElement input, String value) {
    input..focus()
         ..dispatchEvent(new TextEvent('textInput', data: value));
  }
+3

All Articles