Hakan Kiyar
Hakan Kiyar

Reputation: 1209

Vaadin: How To programmatically perform a KeyPressEvent on TAB-Button?

is there a way to programmatically perform a Button Press Event i.e for the TAB-Button in Vaadin? I have to write a test for a ShortCutListener, which listens to ShortCut ShortCutAction.KeyEvent.TAB.

I have tried something like that:

Button button = new Button();

        button.addShortcutListener(new ShortcutListener("ShortCut", ShortcutAction.KeyCode.TAB, null) {

            private static final long serialVersionUID = 1L;

            @Override
            public void handleAction(Object sender, Object target) {
                System.out.println("Click!");

            }
        });

        button.setClickShortcut(ShortcutAction.KeyCode.TAB, null);

        button.click();

Upvotes: 0

Views: 2146

Answers (1)

Valentin
Valentin

Reputation: 11802

If what you want is triggering the click event when pressing the tab key, you could do the following:

Button button = new Button();

button.addClickListener(new Button.ClickListener() {
    private static final long serialVersionUID = 1L;

    @Override
    public void buttonClick(final ClickEvent event) {
        System.out.println("Click!");
    }
});

button.setClickShortcut(ShortcutAction.KeyCode.TAB);

button.click();

Using a Vaadin Button to do something useful on a key press is probably not a good idea, except if the keypress is a shortcut to clicking on the button (which the setClickShortcut method lets you define).


If you want to do something specific on a keypress, something that is different from what your buttons do, you should define an action handler on your Window or Panel, as Vaadin recommends.

Upvotes: 1

Related Questions