Keyboard navigation & shortcut

44 views
Skip to first unread message

Chrisnofneur

unread,
Nov 23, 2009, 12:42:30 PM11/23/09
to Google Web Toolkit
Hi,

I am building a GWt app which has the following layout:

- FlowPanel
* Menu Bar
* Grid (2,x)

I want to add keyboard navigation to the Grid (UP, DOWN & DELETE)

I call the following code in the constructor of my Widget:

// EVENTS
Event.addNativePreviewHandler(new NativePreviewHandler() {

@Override
public void onPreviewNativeEvent(final NativePreviewEvent
event) {

// Key pressed
if (event.getTypeInt() == Event.ONKEYDOWN) {
// Update Grid status (up, down, delete)
}
}
});

When I press UP & DOWN keys, no problem I get the events.
The problem is that navigation in the MenuBar also fires key events
which are catched by this code.
So when I select the MenuBar I have a double navigation: in my MenuBar
and in my Grid

The questions are :
- How can I prevent my code to catch events which target elements like
MenuBar & TextField ?
- Is there another way to deal with my problem ?

Thanks

Chris

Thomas Broyer

unread,
Nov 23, 2009, 6:00:04 PM11/23/09
to Google Web Toolkit
You'd have to test whether MenuBar.getElement().isOrHasChild(Element.as
(event.getNativeEvent().getEventTarget()))

> - Is there another way to deal with my problem ?

Why not use event bubbling (aka event delegation) instead of event
capture? i.e. either:
- put the Grid into a FocusPanel and addKeyDownHandler(...)
- extend Grid and addDomHandler(..., KeyDownEvent.getType())
(eventually implementing HasKeyDownHandlers if you don't want to
handle the KeyDown within the Grid but in the widget that uses the
Grid)

The user would "just" have to click the Grid (or FocusPanel) to have
her keyboard interact with the grid (i.e. as soon as she clicks the
MenuBar or anywhere outside the grid, the handler is no longer called)

Chrisnofneur

unread,
Nov 24, 2009, 8:48:01 AM11/24/09
to Google Web Toolkit
Thank you Thomas.

I've tried those two tricks.

The second one didn't work because I have a button in each cell of my
grid and I don't feel to subclass Button to handle this case.
The first one worked perfectly, I just had to check if my Menu and the
subMenus it contains are target of the event.

I think I'll try to build someting around this.

Chris
Reply all
Reply to author
Forward
0 new messages