Reputation: 31749
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?
Upvotes: 4
Views: 80777
Reputation: 21
As it sounds weird, but try to imagine that new line means '\n' as a character or the character is "Enter" button on the keyboard. So, in code it look like this:
if(event.getKeyChar()=='\n'){
System.out.println("You just pressed ENTER!");
}
Upvotes: 0
Reputation: 13679
In windows with java 7 returns 10 for both cases.
And to display the number value of a char you need cast it to int.
System.out.println((int) e.getKeyChar());
Upvotes: 0
Reputation: 15359
You need to either use event.getNativeEvent().getKeyCode()
or switch to KeyDownHandler
.
newSymbolTextBox.addKeyPressHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
System.out.println("foo =" + KeyCodes.KEY_ENTER);
System.out.println("bar =" + event.getNativeKeyCode());
}
});
See http://code.google.com/p/google-web-toolkit/issues/detail?id=5558
Upvotes: 8
Reputation: 301
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.
Upvotes: 8
Reputation: 4597
KeyCodes.KEY_ENTER
is the integer value of the Enter key. event.getCharCode(
) is the character representation, so it would show as a newline after bar =
Upvotes: 0