我想在我的测试中使用特定的键代码触发 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'));
我该怎么做?
最佳答案
您可以使用 dart:html 中的 KeyEvent 类,如下所示:
dispatchEvent(new KeyEvent(event,
keyCode: keyCode,
charCode: charCode,
altKey: altKey,
ctrlKey: ctrlKey,
metaKey: metaKey,
shiftKey: shiftKey)
.wrapped)
关于dart - 如何在我的 AngularDart 5 测试中使用 KeyCode 触发 KeyDown 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51895324/