我想在测试中使用特定的键代码触发KeyDown事件。我在KeyEvent
文档中(在html_dart2js.dart
文件中)找到了这个示例:
// 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));
但是它不起作用,因为stream
没有add
方法。
我可以做document.dispatchEvent
,但不允许发送密钥代码:
document.dispatchEvent(KeyboardEvent('keydown'));
我该怎么办?
答案 0 :(得分:1)
您可以使用dart:html中的KeyEvent类,如下所示:
dispatchEvent(new KeyEvent(event,
keyCode: keyCode,
charCode: charCode,
altKey: altKey,
ctrlKey: ctrlKey,
metaKey: metaKey,
shiftKey: shiftKey)
.wrapped)