How to implement OnEnter Key shortcut on vaadin-textfield

Hello,

how can I implement an on-enter key shortcut for a vaadin-textfield?

Thanks!

Greetings!

Please look at https://vaadin.com/forum/message/77600

Thanks for your reply!

I can’t find any ShortcutListener in Vaadin Flow.

I found it!
See https://github.com/vaadin/flow/pull/3110

Example on TextField:

textField.addListener(KeyDownEvent.class, event ->
{
	if (Key.ENTER.equals(event.getKey()))
	{
		// ... handle on enter
	}
});

Henrik A:
I found it!
See https://github.com/vaadin/flow/pull/3110

Example on TextField:

textField.addListener(KeyDownEvent.class, event ->
{
	if (Key.ENTER.equals(event.getKey()))
	{
		// ... handle on enter
	}
});

This doesn’t seem to work on Internet Explorer/Microsoft Edge.

I am using the release candidate 5 of Vaadin Flow. There is no addListener method anymore but a method called addKeyDownListener.

I cannot use the condition of the example (Key.ENTER.equals(event.getKey())) since Key has been converted to an interface and ENTER does not override the equals method.

I can write event.getKey().matches("Enter") but is this the recommended way? Do I have to use a String literal instead of an enum or constant?

This worked for me on Vaadin 12.0.2

textField.addKeyDownListener(com.vaadin.flow.component.Key.ENTER, (ComponentEventListener<KeyDownEvent>) keyDownEvent -> doSomething());

try


textfield/passwordfield.setValueChangeMode(ValueChangeMode.EAGER);

UI.getCurrent().addShortcutListener(
                () -> login.click(),
                Key.ENTER);