有没有办法在 GWT 中捕获键盘事件?
我试图在 GWT 中捕获按键按下,并且我尝试了 addKeyDownHandler 和 onBrowserEvent。问题是,只要适当的小部件获得焦点,它们就可以工作,但当其他小部件获得焦点时,它们就会停止工作。我可以将代码添加到每个小部件中,但这看起来很疯狂。我还尝试强制一个小部件始终具有焦点,但这会导致文本框(需要焦点来打字)等其他问题。
是否有一种简单、高级的方法可以在 GWT 中按下某个键时触发某些代码,而不管哪个小部件具有焦点?
I'm trying to trap key downs in GWT, and I've tried both addKeyDownHandler and onBrowserEvent. The problem is that they work as long as the appropriate widget had the focus, but it stops working when something else has the focus. I could add the code to every single widget, but that seems crazy. I also tried forcing one single widget to always have the focus, but that causes other problems with things like TextBoxes (which need the focus for typing).
Is there just a simple, high-level way to trigger some code when a key is pressed in GWT, regardless of which widget has the focus?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Event.addNativePreviewHandler
,或您要附加的KeyDownHandler
RootPanel.get()
(使用addDomHandler
:RootPanel.get().addDomHandler(myHandler, KeyDownEvent.getType())
),具体取决于您要在捕获还是气泡中监听> 阶段(捕获阶段在 IE 中模拟)。You can use either
Event.addNativePreviewHandler
, or aKeyDownHandler
that you'd attach toRootPanel.get()
(usingaddDomHandler
:RootPanel.get().addDomHandler(myHandler, KeyDownEvent.getType())
), depending on whether you want to listen in the capture or bubble phase (the capture phase is emulated in IE).为什么不像这样将 keyDownHandler 添加到 body 元素并将处理程序添加到其中?所有 javascript 事件都会冒泡,除非您在
function()
中阻止它。示例:
RootPanel.getBodyElement()
然后包装到一个类中,使其成为一个 Widget 并像描述的那样添加DomHandler 此处。或者,如果这证明不是很简单,那么只需在托管页面中编写为 javascript,或使用本机方法(GWT 允许您使用本机关键字编写 javascript)。
Why don't you add the keyDownHandler to the body element like this and add the handler to that? All javascript events will bubble down unless you prevent it in a
function()
.Example :
RootPanel.getBodyElement()
Then wrap into a class to make it a Widget and addDomHandler like described here. Or if this proves not very straightforward, then simply write as javascript in the hosted page, or use native methods(GWT allows you to write javascript using native keyword).