views:

35

answers:

1

Hi everyone,

I'm trying to add keyboard shortcuts to a GWT menu, same as in most windows applications (for example ALT + F, then O -> activates the "File" menu, then "Open" MenuItem ).

I know it may conflict with browser shortcuts, so I'm interested in a way of disabling those too...

From what I can see google has all kinds of shortcuts in their applications so, there must be a way to do this.

Thanks!

Edit

Thanks to Igor's response I'm able to capture keyboard input before being consumed by other controls.

What I don't know is how to make the MenuBar show itself (like when mouse hovering). MenuBar doesn't seem to have a method .open() :(

+2  A: 

I haven't actually tried this, but NativeEvent seems to be what you are looking for. You get to it via Event.addNativePreviewHandler(Event.NativePreviewHandler):

Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
    @Override
    public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
        nativeEvent nativeEvent = event.getNativeEvent();
        // Do all sort of cool stuff with nativeEvent
    }
});

Reference: GWT Google Group thread


To select a menu item programatically, use MenuBar.selectItem(MenuItem item) - you'll probably need to keep track of the relevant MenuItems. From the selected MenuItem you can get to its sub menu via MenuItem.getSubMenu() and so on... :) You can play around with the auto-open setting (MenuBar.setAutoOpen(boolean autoOpen) to get it to work like you envisioned.

Igor Klimer
Thank you, this points me in the right direction. Seems to work fine on Firefox, but as usual IE is a bitch again. Doesn't fire events for "special" keys (like ALT, CTRL).
Bogdan
You mean, when *only* Alt is pressed? For combinations with other keys, `getAltKey()` should work.
Igor Klimer
No. In IE, ALT + i or CTRL + i doesn't work. Just "i" works, however. For what I need right now I can live with a direct shortcut (without Alt, or Ctrl).
Bogdan
No event is generated when you press Ctrl + i? If so, this warrants a bug report (http://code.google.com/p/google-web-toolkit/issues/list) :)
Igor Klimer
Hmm... I tried with selectItem, and indeed it selects the passed item, but the menu doesn't open (I only see the parent menuItem highlighted). Tried with setAutoOpen true or false, no change.
Bogdan
How about the submenu - does it have auto-open enabled? Because, the way I see it, that's what you want to open when you select the item (MenuBar1 -> MenuItem -> MenuBar2, MenuBar1 is always showing, it's the top menu bar, MenuBar2 is shown when MenuItem is selected). So, MenuBar2 should have auto-open enabled. Unfortunately, I can't test this right now :/
Igor Klimer
Tried this; no change :(I'm thinking of a different approach. Is is possible to simulate a mouse over event? If I could send a fake mouse over event to the menuBar it should work... But I have no idea on how to do that
Bogdan
`MenuBar.fireEvent` with the appropriate event or maybe `DomEvent.fireNativeEvent` (but I don't know where you'd get a `NativeEvent` from). You could create a new question, if this didn't help (or maybe check if there's a related question on SO, I think there was...).
Igor Klimer
Thanks for all your help. I'll look into this some more.
Bogdan