Listening for <enter> key in a TextArea or TextField

Hi all,

I’m using Vaadin 8.3.1. I’m trying to listen for an event where the user presses the enter key. The Handler solution only seems to work for TextField:

public static class EnterKeyHandler implements Handler {

	private static final long serialVersionUID = 1L;

	private Action enterKeyShortcutAction = new ShortcutAction(null, ShortcutAction.KeyCode.ENTER, null);

	@Override
	public Action[] getActions(Object target, Object sender) {
		return new Action[] { enterKeyShortcutAction };
	}

	@Override
	public void handleAction(Action action, Object sender, Object target) {
		System.out.println("onEnterKey");
	}
}

But nothing seems to happen when enter is pressed in a TextArea. I tried adding a ShortcutListener but that didn’t work for either TextField or TextArea:

tf.addShortcutListener(new ShortcutListener("Enter", KeyCode.ENTER, null) {

	private static final long serialVersionUID = 1L;

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

Any thoughts?

Thanks!

William

You could probably use this add-on: https://vaadin.com/directory/component/keyactions

-Olli

Thanks. Are you saying that you do not know of a built-in Vaadin way of doing this?

I just tried using the KeyActions add-on and I’m now getting the following error when compiling the widgetset:

[INFO]
 Scanning for projects...
[INFO]
                                                                         
[INFO]
 ------------------------------------------------------------------------
[INFO]
 Building rom 1.0-SNAPSHOT
[INFO]
 ------------------------------------------------------------------------
[INFO]
 
[INFO]
 --- vaadin-maven-plugin:8.2.0:update-widgetset (default-cli) @ rom ---
[WARNING]
 GWT plugin is configured to detect modules, but none were found.
[INFO]
 No widgetsets found - generating AppWidgetset if necessary.
[INFO]
 Updating widgetset AppWidgetset
[INFO]
 Adding resource directory to command classpath: C:\Users\William\git\erom\ROM\src\main\resources
[INFO]
 Adding resource directory to command classpath: C:\Users\William\git\erom\ROM\target\generated-resources\gwt
[INFO]
 Using com.vaadin:vaadin-client version 8.3.1
[INFO]
 Using com.vaadin:vaadin-client-compiler version 8.3.1
[INFO]
 Widgetsets found from classpath:
[INFO]
 	org.vaadin.artur.WidgetSet in jar:file:C:/Users/William/.m2/repository/org/vaadin/artur/KeyActions/1.0.0/KeyActions-1.0.0.jar!/
[INFO]
 	com.vaadin.DefaultWidgetSet in jar:file:C:/Users/William/.m2/repository/com/vaadin/vaadin-client/8.3.1/vaadin-client-8.3.1.jar!/
[INFO]
 Addon styles found from classpath:
[INFO]
 
[INFO]
 Search took 17ms
[INFO]
 
[INFO]
 --- vaadin-maven-plugin:8.2.0:compile (default-cli) @ rom ---
[INFO]
 auto discovered modules [AppWidgetset]

[INFO]
 Using com.vaadin:vaadin-client version 8.3.1
[INFO]
 Using com.vaadin:vaadin-client-compiler version 8.3.1
[INFO]
 Compiling module AppWidgetset
[INFO]
 [ERROR]
 An internal compiler exception occurred
[INFO]
 com.google.gwt.dev.jjs.InternalCompilerException: Error constructing Java AST
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder.translateException(GwtAstBuilder.java:3997)
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder$AstVisitor.endVisit(GwtAstBuilder.java:1569)
[INFO]
 	at org.eclipse.jdt.internal.compiler.ast.MessageSend.traverse(MessageSend.java:1021)
[INFO]
 	at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.traverse(LocalDeclaration.java:307)
[INFO]
 	at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.traverse(MethodDeclaration.java:347)
[INFO]
 	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1490)
[INFO]
 	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1363)
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder.processImpl(GwtAstBuilder.java:3933)
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder.process(GwtAstBuilder.java:3971)
[INFO]
 	at com.google.gwt.dev.javac.CompilationStateBuilder$CompileMoreLater$UnitProcessorImpl.process(CompilationStateBuilder.java:129)
[INFO]
 	at com.google.gwt.dev.javac.JdtCompiler$CompilerImpl.process(JdtCompiler.java:336)
[INFO]
 	at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:470)
[INFO]
 	at com.google.gwt.dev.javac.JdtCompiler.doCompile(JdtCompiler.java:1040)
[INFO]
 	at com.google.gwt.dev.javac.CompilationStateBuilder$CompileMoreLater.compile(CompilationStateBuilder.java:325)
[INFO]
 	at com.google.gwt.dev.javac.CompilationStateBuilder.doBuildFrom(CompilationStateBuilder.java:548)
[INFO]
 	at com.google.gwt.dev.javac.CompilationStateBuilder.buildFrom(CompilationStateBuilder.java:479)
[INFO]
 	at com.google.gwt.dev.javac.CompilationStateBuilder.buildFrom(CompilationStateBuilder.java:465)
[INFO]
 	at com.google.gwt.dev.cfg.ModuleDef.getCompilationState(ModuleDef.java:423)
[INFO]
 	at com.google.gwt.dev.Precompile.precompile(Precompile.java:222)
[INFO]
 	at com.google.gwt.dev.Precompile.precompile(Precompile.java:202)
[INFO]
 	at com.google.gwt.dev.Precompile.precompile(Precompile.java:143)
[INFO]
 	at com.google.gwt.dev.Compiler.compile(Compiler.java:204)
[INFO]
 	at com.google.gwt.dev.Compiler.compile(Compiler.java:155)
[INFO]
 	at com.google.gwt.dev.Compiler.compile(Compiler.java:144)
[INFO]
 	at com.google.gwt.dev.Compiler$1.run(Compiler.java:118)
[INFO]
 	at com.google.gwt.dev.CompileTaskRunner.doRun(CompileTaskRunner.java:55)
[INFO]
 	at com.google.gwt.dev.CompileTaskRunner.runWithAppropriateLogger(CompileTaskRunner.java:50)
[INFO]
 	at com.google.gwt.dev.Compiler.main(Compiler.java:125)
[INFO]
 Caused by: java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.ast.MessageSend.binding()Lorg/eclipse/jdt/internal/compiler/lookup/MethodBinding;
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder.isUncheckedGenericMethodCall(GwtAstBuilder.java:4220)
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder.access$1400(GwtAstBuilder.java:250)
[INFO]
 	at com.google.gwt.dev.jjs.impl.GwtAstBuilder$AstVisitor.endVisit(GwtAstBuilder.java:1562)
[INFO]
 	... 26 more
[INFO]
    [ERROR]
 at ClickableRenderer.java(182): WidgetUtil.findWidget(e)
[INFO]
       org.eclipse.jdt.internal.compiler.ast.MessageSend
[INFO]
 ------------------------------------------------------------------------
[INFO]
 BUILD FAILURE
[INFO]
 ------------------------------------------------------------------------
[INFO]
 Total time: 15.053 s
[INFO]
 Finished at: 2019-02-05T10:53:09-04:00
[INFO]
 Final Memory: 27M/339M
[INFO]
 ------------------------------------------------------------------------
[ERROR]
 Failed to execute goal com.vaadin:vaadin-maven-plugin:8.2.0:compile (default-cli) on project rom: Command [[
[ERROR]
 C:\Program Files\Java\jre1.8.0_144\bin\java -Xmx1G -Dgwt.persistentunitcache=false com.google.gwt.dev.Compiler -logLevel INFO -style OBF -war C:\Users\William\git\erom\ROM\target\classes\VAADIN\widgetsets -localWorkers 4 -failOnError -XfragmentCount -1 -sourceLevel auto -gen C:\Users\William\git\erom\ROM\target\.generated AppWidgetset
[ERROR]
 ]] failed with status 1
[ERROR]
 -> [Help 1]

[ERROR]
 
[ERROR]
 To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR]
 Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
 
[ERROR]
 For more information about the errors and possible solutions, please read the following articles:
[ERROR]
 [Help 1]
 http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

When I remove the KeyActions from my pom.xml then the widgetset compiles fine. I’m using Vaadin 8.3.1.

Thanks. Are you saying that you do not know of a built-in Vaadin way of doing this?

Not sure without investigating, but I’m guessing the TextArea is eating the enter key presses and the add-on could work around that maybe.

Could you be facing this error: https://stackoverflow.com/questions/41039097/gwt-error-constructing-java-ast ?

Thanks a bunch - that solved it. I had to use the latest version of gwt-dev though (2.8.0 gave a different error):

<dependency>
	<groupId>com.google.gwt</groupId>
	<artifactId>gwt-dev</artifactId>
	<version>2.8.2</version>
	<scope>provided</scope>
</dependency>