Javid Jamae
Javid Jamae

Reputation: 9029

Why isn't the keypress event being sent to GWT's TextInputCell.onBrowserEvent() method?

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.

Upvotes: 1

Views: 2715

Answers (1)

Javid Jamae
Javid Jamae

Reputation: 9029

So it turns out that the AbstractCell takes a list of strings that represent the "consumedEvents". This basically limits the event types that can be handled by the child cell classes. TextInputCell passes in the change and keyup event types. The AbstractInputCell adds focus, blur, and keydown. Therefore, TextInputCell is not able to handle keypress native events.

I ended up creating a near-exact copy of the TextInputCell in my own project and adding the keypress type to the constructor. I was then able to grab the events and do the filtering that I wanted to do. I'm going to create a request in GWT to add this functionality into the TextInputCell.

I also created a feature request to add this functionality to GWT: http://code.google.com/p/google-web-toolkit/issues/detail?id=5891

Upvotes: 2

Related Questions