Search code examples
javagwt

Trying to get the char code of ENTER key


I have this this code:

newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
  public void onKeyPress(KeyPressEvent event) {
      System.out.println("foo =" + KeyCodes.KEY_ENTER);
  System.out.println("bar =" + event.getCharCode());
  }
});

When I press ENTER I get this output:

foo =13
bar =

I expected a value after bar =. Any idea?


Solution

  • The value is, in fact, there. The reason you don't see anything is because the character is a carriage return. The carriage return, I believe, just moves the next line (you can google it's exact function). If you google "ascii table" you will see that the 13 you get from KeyCodes.KEY_ENTER corresponds with CR, the carriage return character.