The question:
I'm overriding the GWT TextInputCell.onBrowserEvent()
method to try and apply filtering on the characters that are typed in to the field. How can I can't get the actual character that the user typed in on a keydown
event?
What I've tried:
I can use the NativeEvent.getKeyCode()
method to get the key code, but this is insufficient because I want to know the specific character they typed in, so I can compare the character to a regular express. As I understand it, the keyCode just tells me the key they typed on the keyboard, so I can't discriminate between a capital 'A' and a lowercase 'a'. I can also grab the value from the InputElement
on the keyup
event, but this is too late, because the character has already been rendered to the UI element.
When I call NativeEvent.getCharCode()
it returns 0 for any character I type. I tried this on Chrome and on Firefox. Why does getCharCode()
always return 0? Is this a bug?
For example, here is what I tried and the values I get when I type in the number '1' on the keyboard:
event.getKeyCode() - 49
event.getCharCode() - 0
Character.toChars(event.getCharCode())[0] - (a box character because charCode was 0)
String.valueOf(Character.toChars(event.getCharCode())) - (a box character because charCode was 0)
String.valueOf((char) event.getKeyCode()) - 1
Update:
OK, so I see that I need to capture the keypress
event and not keydown
, but I print off every event.getType()
value for every event that occurs as I type and I never see a single keypress
event triggered. So my updated question is, why isn't the keypress
event occurring? And if I can't capture it in the TextInputCell.onBrowserEvent()
method, is there a different way to capture it for a TextInputCell
.