Vaadin:如何以编程方式在 TAB 按钮上执行 KeyPressEvent? [英] Vaadin: How To programmatically perform a KeyPressEvent on TAB-Button?
问题描述
有没有办法以编程方式执行按钮按下事件,即 Vaadin 中的 TAB
-Button?我必须为 ShortCutListener 编写一个测试,它监听 ShortCut ShortCutAction.KeyEvent.TAB
.
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
.
我尝试过类似的事情:
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();
推荐答案
如果你想要的是在按下 tab 键时触发 click 事件,你可以执行以下操作:
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();
使用 Vaadin Button
在按键上做一些有用的事情可能不是一个好主意,除非按键是点击按钮的快捷方式(setClickShortcut
方法让你定义).
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).
如果你想在按键上做一些与你的按钮不同的事情,你应该在你的 Window
或 Panel
上定义一个动作处理程序,如Vaadin 推荐.
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.
这篇关于Vaadin:如何以编程方式在 TAB 按钮上执行 KeyPressEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!