Search code examples
gwtgwt2

Mnemonics in GWT


I would like to create as Java Swing Mnemonics with GWT . But I don't know how to figure it out. I have googled for it but I didn't fond any sample codes for it . I want to bind some keyboard shortcut keys on my buttons. How can I achieve it ? Any suggestions would be really appreciated !


Solution

  • In general you can handle global keyboard shortcusts using a NativePreviewHandler. An example of this can you see here:

    NativePreviewHandler nativePreviewHandler = new NativePreviewHandler() {
    
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (event.getTypeInt() != Event.ONKEYDOWN) {
                return;
            }
            final NativeEvent nativeEvent = event.getNativeEvent();
            final boolean altKey = nativeEvent.getAltKey();
            final boolean ctrlKey = nativeEvent.getCtrlKey();
            if(altKey && ctrlKey && nativeEvent.getKeyCode() == 'A') {
                // Do Something
            }
        }
    };
    Event.addNativePreviewHandler(nativePreviewHandler);
    

    But as far as I klnow, there's no generic way build into GWT to handle some kind of Action that is bound to a button/Menu as well as a keyboard shortcut. You will have to implement such an abstraction by yourselves.