views:

28

answers:

1

In our RCP application, we need to resort to using a global key event handler (via Display.addFilter()) for more advanced key event handling/routing irrespective of current focus. We need to be able to determine if a dialog box is currently open for some of the routing logic.

Seems like a fairly trivial question but I keep hitting dead ends going off Widget hierarchy, Shells, WindowManagers.

I am looking for a robust solution that would not require any extra work on the part of Dialog implementers or client code that uses standard framework dialogs.

+1  A: 

In the example below, shell is a defined Shell in the scope. You could modify the code to compare activeShell with a list of Shells.

shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
    public void handleEvent(final Event event) {
        if (shell.isDisposed()) {
            return;
        }
        final Shell activeShell = shell.getDisplay().getActiveShell();
        if (activeShell != null && activeShell.equals(shell)) {
            if (event.stateMask == SWT.MOD1 && event.character == 'w') {
                shell.dispose();
            }
        }
    }
});

This example code will close shell when +W is pressed on Mac.

Paul Lammertsma
getActiveShell() was the key bit of info I was looking for. In our specific case, we can compare shells for all of the workbench windows to it. Thanks!
Andrei Lissovski