How can I trigger KeyDown event with KeyCode in my AngularDart 5 test?


I want to trigger KeyDown event with specific key code in my test. I’ve found this example in KeyEvent documentation (into html_dart2js.dart file):

// Initialize a stream for the KeyEvents:
var stream = KeyEvent.keyPressEvent.forTarget(document.body);
// Add a new KeyEvent of someone pressing the 'A' key to the stream so
// listeners can know a KeyEvent happened.
stream.add(new KeyEvent('keypress', keyCode: 65, charCode: 97));

But it doesn’t work because stream doesn’t have add method.

I can do document.dispatchEvent but it doesn’t allow to send key code:


How can I do it?


You can use the KeyEvent class from dart:html something like this:

dispatchEvent(new KeyEvent(event,
              keyCode: keyCode,
              charCode: charCode,
              altKey: altKey,
              ctrlKey: ctrlKey,
              metaKey: metaKey,
              shiftKey: shiftKey)

Answered By – Ted Sander

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *