简体   繁体   English

如何在vaadin TextArea中检测输入键按下

[英]How to detect enter key press in vaadin TextArea

I am using a vaadin TextArea as a rough console. 我使用vaadin TextArea作为粗略的控制台。 The user can enter commands which should be executed when he presses the enter key. 用户可以输入在按下回车键时应该执行的命令。 Is there a way to specify this with a listener on the TextArea? 有没有办法在TextArea上使用侦听器指定它?

The closest thing I found is to use: 我发现最接近的是使用:

TextArea textArea = new TextArea();
textArea.addTextChangeListener(this);
textArea.setTextChangeEventMode(TextChangeEventMode.EAGER);

And handle the text change event: 并处理文本更改事件:

@Override
public void textChange(TextChangeEvent event) {
   System.out.println(event.getText());
}

This is however triggered as soon as text has been entered in the TextArea. 但是,只要在TextArea中输入了文本,就会触发此操作。 I would like to be notified only when the enter key has been pressed. 我希望只有在按下回车键时才会收到通知。

You cannot listen to shortcut keys on the textarea itself, but a simple solution would be to add a submit button and use enter as it's shortcut: 你不能听textarea本身的快捷键,但一个简单的解决方案是添加一个提交按钮并使用enter作为它的快捷方式:

Button b = new Button("submit", new Button.ClickListener() {
    @Override
    public void buttonClick(ClickEvent event) {
        // handle your event
    }
});
layout.addComponent(b);
b.setClickShortcut(KeyCode.ENTER);

You can hide the button itself if you don't wish it: 如果您不希望它,您可以隐藏按钮本身:

b.setVisible(false);

Another solution would be to use ShortcutActions and Handlers as described in here: https://vaadin.com/book/-/page/advanced.shortcuts.html 另一种解决方案是使用ShortcutActions和Handler,如下所述: https//vaadin.com/book/-/page/advanced.shortcuts.html

But in either case you have to take into account that listening to enter key will cause a conflict when using a TextArea component because you also need to use the same key to get to the next line in the TextArea. 但在任何一种情况下,您都必须考虑到在使用TextArea组件时监听enter键会导致冲突,因为您还需要使用相同的键才能到达TextArea中的下一行。

You can add a ShortcutListener to the TextArea, like this: 您可以将ShortcutListener添加到TextArea,如下所示:

TextArea textArea = new TextArea();
textArea.addShortcutListener(enter);

Now you just have to initialize some ShortcutListener as follows: 现在你只需要初始化一些ShortcutListener,如下所示:

ShortcutListener enter = new ShortcutListener("Enter", KeyCode.ENTER, null) {

    @Override
    public void handleAction(Object sender, Object target) {
        // Do nice stuff
        log.info("Enter pressed");
    }
};

For this, we use the following utility function 为此,我们使用以下实用程序功能

/**
 * Perform the specified action when the text field has focus and `ENTER` is pressed.
 * 
 * @param tf The {@link com.vaadin.ui.TextField text field} or 
 * {@link com.vaadin.ui.TextArea text area)
 * @param action The action to perform
 */
public static void onKeyEnter(AbstractTextField tf, Consumer<AbstractTextField> action) {
    tf.addFocusListener(event -> {
        final Registration r = tf.addShortcutListener(
            new ShortcutListener("Enter", KeyCode.ENTER, null) {

                @Override
                public void handleAction(Object sender, Object target) {
                    // sender: UI, target: TextField
                    assert target == tf;
                    action.accept(tf);
                }
            });
        tf.addBlurListener(e -> r.remove());
    });        
}

To use it: 要使用它:

final TextField searchField = new TextField(); // or TextArea
searchField.setPlaceholder("Search text (ENTER)...");
// ..
onKeyEnter(searchField, tf -> doSearch(tf.getValue()));

//Refactored for vaadin 7 //为vaadin 7重构

 public static void onKeyEnter(AbstractTextField tf, Consumer<AbstractTextField> action) {
    tf.addFocusListener(event -> {
      ShortcutListener scl = new ShortcutListener("Enter", KeyCode.ENTER, null) {
        public void handleAction(Object sender, Object target) {
          assert target == tf;
          action.accept(tf);
        }
      };
      tf.addShortcutListener(scl);
      tf.addBlurListener(e -> tf.removeShortcutListener(scl));
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM