Adding ENTER key shortcut listener in both input field and button

Hi guys,

I have a text field where I want to add ENTER key shortcut listener
I can add it like this:

ShortcutListener shortcut = new ShortcutListener("Shortcut Name", ShortcutAction.KeyCode.ENTER, null) {
		@Override
		public void handleAction(Object sender, Object target) {
			log.info("Enter pressed: INDUSTRY STANDARD");

		}
	};

	searchInput.addFocusListener(new FocusListener() {
		public void focus(FocusEvent event) {
			someButton.addShortcutListener(shortcut);
			log.info("Enter pressed according to INDUSTRY STANDARD");
		}
	});

However my entire app is attached to the someButton

searchBtn.addClickListener((Button.ClickListener) event -> {
			log.info("My applicatin where clicking on the button executes the search app");
			
			});
		

Now how do I make so that both pressing ENTER key
and the button (someButton) executes my search app…

Any ideas

I don’t realy understand. Would something like this not work ?

private void startSearch() {
	//starts your search method
}

ShortcutListener shortcut = new ShortcutListener("Shortcut Name", ShortcutAction.KeyCode.ENTER, null) {
		@Override
		public void handleAction(Object sender, Object target) {
			startSearch();
		}
	};
	
	searchBtn.addClickListener((Button.ClickListener) event -> startSearch());

Thank you for your response. It works but with some twists and turns. :slight_smile: