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

狂风中的少年 提交于 2019-12-25 05:19:12

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/36131959/vaadin-how-to-programmatically-perform-a-keypressevent-on-tab-button

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