Howto listen for global key down events

2,708 views
Skip to first unread message

David

unread,
Sep 19, 2011, 2:23:00 AM9/19/11
to Google Web Toolkit
How do I listen for key down events in the whole browser window,
independent of what component has the focus? The code below doesn't do
it. What's wrong? Actually I need to bind shortcut keys to components.
I know about the setAccessKey(char) method but this only handles the
alt key (Windows). I need to bind other combinations like combinations
including ctrl. Is this possible with GWT and how could it be done?

RootPanel.get().addHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
GWT.log("onKeyDown charCode=" + event.getNativeKeyCode());
}
}, KeyDownEvent.getType());


Any help appreciated, thanks!

Y2i

unread,
Sep 19, 2011, 2:34:55 AM9/19/11
to google-we...@googlegroups.com
Have you tried Event.addNativePreviewHandler() ?

David

unread,
Sep 19, 2011, 4:29:55 AM9/19/11
to Google Web Toolkit
Thanks Y2i!

Event.addNativePreviewHandler() does it (see code below)! However,
alt, ctrl, and meta doesn't seam to work (shift works)!? Since
FocusWidget.setAccessKey(char) handles key combos with the alt key GWT
should be able to listen for alt, right? But how? And how do I handle
ctrl and meta? (I have tested this with devmode and Chrome.)

Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
public void onPreviewNativeEvent(NativePreviewEvent event) {
NativeEvent ne = event.getNativeEvent();
GWT.log(ne.getCharCode() + " (" + ((char) ne.getCharCode()) + ") " +
(ne.getButton() != 1 ? " button=" + ne.getButton() : "") +
(ne.getKeyCode() != ne.getCharCode() ? " keyCode=" +
ne.getKeyCode() : "") +
(ne.getAltKey() ? " ALT" : "") +
(ne.getCtrlKey() ? " CTRL" : "") +
(ne.getMetaKey() ? " META" : "") +
(ne.getShiftKey() ? " SHIFT" : ""));
}
});


On Sep 19, 8:34 am, Y2i <yur...@gmail.com> wrote:
> Have you tried Event.addNativePreviewHandler() <http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/googl...)>
> ?

On Sep 19, 8:34 am, Y2i <yur...@gmail.com> wrote:
> Have you tried Event.addNativePreviewHandler() <http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/googl...)>
> ?

Sarjith Pullithodi

unread,
Oct 25, 2011, 7:02:17 AM10/25/11
to google-we...@googlegroups.com


On Mon, Sep 19, 2011 at 12:04 PM, Y2i <yur...@gmail.com> wrote:
Have you tried Event.addNativePreviewHandler() ?


I was using this option to define short cut keys in the screen. but i have 2-3 screens in my application. so this short cut keys should work differently on screen by screen. So I had to define NativePreviewHandler in each screen, and remove them when application unload that screen (to avoid NativePreviewHandler being fired again even after quiting from that screen).

but problem arose when I wished to define a common header widget. (just like we use header.jsp).
I want to define a few shortcuts where ever this header has been used. but it will be a burden to remove NativePreviewHandler wherever it has been used. ie, this header should be independant.

Any idea or workaround to achieve this?.

Thanks.

--
Sarjith
 

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/wzgBDGYEFXAJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Thomas Broyer

unread,
Oct 25, 2011, 7:13:02 AM10/25/11
to google-we...@googlegroups.com
How about using a single NativePreviewHandler into which "screens" (or other widgets) will "register"? Or how about having thins NativePreviewHandler singleton fire events on the EventBus, and have "screens" and other widgets add handlers to the EventBus?

(BTW, Activities make it easy to manage the lifecycle of your "screen parts", which makes it easy to register/unregister things –it's even easier with the EventBus, as you don't even have to explicitly remove your handlers–)
Reply all
Reply to author
Forward
0 new messages