Enter Key in Extension


i’m having a little when trying to write a component extension which listens to the Enter key getting pressed.
I already added a KeyPressHandler inside the extend method of the ExtensionConnector (using addDOMHandler) and it is working fine in the way that key presses execute the onkeypress method.
The only problem is that when i try to get the key value from the keypressevent and try check if it was the Enter key the data returned is similar to the Backspace key, tab key, space key, arrow keys, … etc.

When i use event.getCharCode it returns an empty value/or a space for all of the keys and when i use getUnicodeCharCode i get a 0 for all of them.

Is there a way to check if specifically the enter key was pressed inside the connector?

Best Regards,


I remember encountering some issue like this in the past, please try calling KeyPressEvent.getNativeEvent().getKeyCode(). If that doesn’t work, you could try a KeyDownHandler or KeyUpHandler instead of KeyPressHandler.


Just wanted to add that Vaadin seems to internally use key down events for the shortcuts.

Thank you Teppo for your quick reply.

KeyPressEvent.getNativeEvent().getKeyCode() worked like a charm and for future reference:
KeyDownEvent.getNativeKeyCode() did too.
I didn’t test KeyUpEvent but i’m guessing it does too.

Again thank you for your help.