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?
You can use either Event.addNativePreviewHandler
, or a KeyDownHandler
that you'd attach to RootPanel.get()
(using addDomHandler
: 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With