本文介绍了如何调度具有特定KeyCode的KeyboardEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图为esc键分配键盘事件(用于测试)这是我到目前为止:I try to dispatch an keyboard event for the esc key (for testing)this is what i got so far: KeyboardEvent keyEvent = new KeyboardEvent('keypress'); window.onKeyPress.listen((KeyboardEvent event){ KeyEvent keyEvent = new KeyEvent.wrap(event); if(keyEvent.keyCode == KeyCode.ESC){ //do stuff } }); window.dispatchEvent(keyEvent);这样可以正常工作。 onKeyPress侦听器触发。This works as expected. The onKeyPress listener triggers.但我没有找到如何设置KeyCode为我的KeyBoardEvent? But i didn't find out how to set a KeyCode for my KeyBoardEvent? 推荐答案无法使用 dispatchEvent 。尝试: Can't use dispatchEvent. Try:import 'dart:async';import 'dart:html';void main() { Stream 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));} https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:html.KeyEvent 这篇关于如何调度具有特定KeyCode的KeyboardEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 15:27