Issue
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:
document.dispatchEvent(KeyboardEvent('keydown'));
How can I do it?
Solution
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)
.wrapped)
Answered By – Ted Sander
Answer Checked By – David Goodson (FlutterFixes Volunteer)