SWT issues with cascaded MenuItem accelerators

元气小坏坏 提交于 2019-12-10 11:52:12

问题


The MenuItem objects having a SWT.CASCADE style (like 'File') won't drop down when the accelerator key combo is pressed.

In the example below, pressing Alt-F triggers the Selection event (I see "File" in the console) but the menu itself won't drop down. I couldn't find a method to programmatically make the menu drop down either. Any idea?

(The package I'm using is org.eclipse.swt.win32.win32.x86_64_3.100.0.v4233d.jar provided with the current version of Eclipse Juno.)

public class MenuTest {
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Menu menu = new Menu(shell, SWT.BAR);

        MenuItem item = new MenuItem(menu, SWT.CASCADE);
        item.setText("File");
        item.setAccelerator(SWT.ALT | 'F');
        Menu dropMenu = new Menu(shell, SWT.DROP_DOWN);

        item.setMenu(dropMenu);
        item.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                System.out.println("File");
            }
        });

        item = new MenuItem(dropMenu, SWT.NULL);
        item.setText("Close");
        item.setAccelerator(SWT.ALT | SWT.F4);
        item.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                System.out.println("Close");
                shell.dispose();
            }
        });

        shell.setMenuBar(menu);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

回答1:


There is nothing wrong with your code and it is working as expected. By default on Windows OS, the Menu bars receive input focus when users press the Alt key and they lose input focus with the Esc key (see Keyboard accessibility section).

Now an accelerator is like a hot key/combination which is there to make ones life easier. In your scenario, when you press the ALT + F then default behavior of ALT of setting focus on the menu is over-ridden. And, that is why the menu listener is fired but the drop down menu is not shown.

Possible Solution

  1. Instead of item.setAccelerator(SWT.ALT | 'F'); do something like this item.setAccelerator(SWT.MOD1 | 'F');. Where MOD1, MOD2 .. MOD4 are the keyboard and/or mouse event mask indicating that the MOD1 key was pushed on the keyboard when the event was generated.
  2. On Windows OS you need not to set an accelerator for topmost level menu. Instead use mnemonic. For example, item.setText("&File");. See javadoc of setText() for more details.
  3. Or, more drastically How to disable normal behaviour of Alt key? (its a C++ solution)

Result of mnemonic

See the underline under the F of File.



来源:https://stackoverflow.com/questions/12219537/swt-issues-with-cascaded-menuitem-accelerators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!