开发者

Is there any way of adding shortcut keys for MenuBar/MenuItem elements in GWT?

开发者 https://www.devze.com 2023-01-09 21:28 出处:网络
I\'m trying to add keyboard shortcuts to a GWT menu, same as in most windows appli开发者_StackOverflow中文版cations (for example ALT + F, then O -> activates the \"File\" menu, then \"Open\" MenuItem

I'm trying to add keyboard shortcuts to a GWT menu, same as in most windows appli开发者_StackOverflow中文版cations (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() :(


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消