Key Issue Focus Key SWT GlobalListener

for my application I need a space key to call a function independent of the focused widget everywhere in the application, but only if the corresponding tab is open. I found that you can add a filter to the display, for example:

 getShell().getDisplay().addFilter(SWT.KeyDown, new Listener() {

            public void handleEvent(Event arg0) {
                if( arg0.character == 32 ) { /**SPACE*/
                    if( mainTabs.getSelection().equals(analyseSoundFilesTab)) {
                        soundController.playButtonClickHandler();
                    }
                }
            }

        });

This works fine, but if I give the focus button through a tab or shift tab, its strange oddity - a space will activate the button "pressed", as if one click of a button with the mouse. I'm stuck a bit now, I don’t know how to avoid it ... For buttons, I implemented SelectionListener.

Sincerely.

+3
source share
2 answers

TraverseListener , doin. :

display.addFilter(SWT.KeyDown, new Listener() {
    public void handleEvent(Event e) {
        if (e.character == 32) {
            System.out.printf("Space detected %s\n", e);
        }
    }
});

Button b1 = new Button(shell, SWT.PUSH);
b1.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent se) {
        System.out.printf("Button pressed %s\n", se);
    }
});

b1.addTraverseListener(new TraverseListener() {
    @Override
    public void keyTraversed(TraverseEvent te) {
        System.out.printf("Traverse detected %s\n", te);
        te.doit = true;
    }
});

addTraverseListener() , , "Space detected...", "Button ...". , te.doit = true, SWT, ( ) , . te.detail .

+2

" " - , (?) , .

Button, Space.

, , , , - .

+2

Source: https://habr.com/ru/post/1759496/


All Articles