javamenuswtaccelerator

Menu accelerator SWT.ALT+SWT.ARROW is not triggered


I'm using SWT 4.4.2 (win32) to build the Graphical User Interface for a simple mp3 player application.
In a SWT tree I show some folders and files to play.
Now I want to change the volume of a playing file by clicking a menu item and also by pressing ALT+ARROW_UP and ALT+ARROW_DOWN.

So I have these components:

tree = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.SINGLE | SWT.V_SCROLL);
tree.setLinesVisible(true);
tree.setLocation(10, 10);
tree.setSize(240, 440);

audioLouderMenu = new MenuItem(audioVolumeMenu, SWT.PUSH);
audioLouderMenu.setAccelerator(SWT.ALT | SWT.ARROW_UP);
audioLouderMenu.setText("Louder");
audioLouderMenu.addListener(SWT.Selection, audioVolumeMenuHandler);

audioQuieterMenu = new MenuItem(audioVolumeMenu, SWT.PUSH);
audioQuieterMenu.setAccelerator(SWT.ALT | SWT.ARROW_DOWN);
audioQuieterMenu.setText("Quieter");
audioQuieterMenu.addListener(SWT.Selection, audioVolumeMenuHandler);

The problem is that if the tree view has the focus and I press ALT+ARROW_DOWN then another entry in the tree will be selected and not the shortcut of the menu item is triggered.

Is this a bug of SWT and how can I solve the problem? I know that I can define another shortcut but I think that it should also be possible to use ALT+ARROW_UP/DOWN...


Solution

  • This isn't a bug but a (platform-dependant) keyboard navigation feature of the tree.

    I recommend to choose a different shortcut for the audio volume in order to not confuse your users. If you nonetheless want to use ALT+ARROW_DOWN, you can try to prevent the tree from consuming this key combination and thus give the menu a chance to consume it:

    tree.addListener( SWT.KeyDown, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        if( event.keyCode == SWT.ARROW_DOWN && ( event.stateMask & SWT.MOD3 ) != 0 ) {
          event.doit = false;
        }
      }
    } );