如何模拟浏览器中的击键

时间:2016-07-24 18:34:24

标签: dart keyevent dart-html

嗨,我似乎无法找到模拟关键事件的正确方法,我遇到了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);
 }

我得到的最好的是

未捕获的InvalidStateError:无法执行' dispatchEvent' on' EventTarget':事件已被分派。

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

0 个答案:

没有答案