嗨,我似乎无法找到模拟关键事件的正确方法,我遇到了3个不同的选项,但是我没有尝试过,但是没有一个适合我。
//V1
import 'dart:async';
import 'dart:html';
void main() {
var stream = KeyEvent.keyPressEvent.forTarget(document.body);
stream.listen((KeyEvent keyEvent){
if(keyEvent.keyCode == KeyCode.ESC) {
// do stuff
}
});
stream.add(new KeyEvent('keypress', keyCode: KeyCode.ESC));
}
-
//V2
import 'dart:html';
myCustomKeyHandler(KeyEvent e) {
print('The charCode is ${e.charCode}');
print('The keyCode is ${e.keyCode}');
}
main() {
var controller = new KeyboardEventController.keydown(document.window);
controller.add(myCustomKeyHandler);
}
-
//V3
import 'dart:html';
myCustomKeyHandler(KeyboardEvent e) {
print('The charCode is ${e.charCode}');
print('The keyCode is ${e.keyCode}');
}
main() {
document.window.add(myCustomKeyHandler, false);
}
我得到的最好的是
源 https://github.com/dart-lang/sdk/issues/17950
How to dispatch an KeyboardEvent with specific KeyCode
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/mgnd1TUGn68