Textbox event key code not case sensitive?

140 views
Skip to first unread message

rakesh wagh

unread,
Aug 14, 2009, 3:37:01 PM8/14/09
to Google Web Toolkit
Today While extending the TextBox, I encountered very strange
behaviour.
All alphabets returned by event.getNativeKeyCode() are upper case.
Here is sample program:

public class TextBox extends com.google.gwt.user.client.ui.TextBox{
public TextBox() {
super();
addKeyDownHandler(new KeyDownHandler(){
public void onKeyDown(KeyDownEvent event) {
int key = event.getNativeKeyCode();
System.out.println("key: " + (char)key);
}
}
}
}

Enter characters in lower case, it will print upper case to the
console. I thought of checking with the group before raising it as an
issue. Any idea on what's going on here?

Thanks,
Rakesh Wagh

Thomas Broyer

unread,
Aug 14, 2009, 7:31:38 PM8/14/09
to Google Web Toolkit
You're confusing "key codes" with "characters". Key codes (as well as
keydown and keyup events) are really about keyboard *keys* that you
depress (or at least should be, as it differs a bit amongst browsers).

This was really confusing in GWT 1.5 and previous versions, since GWT
1.6 and the new events, it's a bit clearer, but still somewhat buggy
(see issue 3753: http://code.google.com/p/google-web-toolkit/issues/detail?id=3753
)

See also issues 72 (among others): http://code.google.com/p/google-web-toolkit/issues/detail?id=72

Rakesh

unread,
Aug 21, 2009, 3:02:04 PM8/21/09
to Google Web Toolkit
Thomas, I am not sure I understand what you are saying. I need the
last char pressed by the user(on key down/up). How do you get that?

On Aug 14, 6:31 pm, Thomas Broyer <t.bro...@gmail.com> wrote:

Thomas Broyer

unread,
Aug 21, 2009, 6:13:44 PM8/21/09
to Google Web Toolkit


On 21 août, 21:02, Rakesh <rake...@gmail.com> wrote:
> Thomas, I am not sure I understand what you are saying. I need the
> last char pressed by the user(on key down/up). How do you get that?

Use KeyPress.

KeyDown/KeyUp are for the gestures you're doing with your fingers on
your keyboard. For example:
a. you pressed the "A" key while the "Shift" key is depressed
b. you pressed the "A" without any modifier key depressed at the same
time
In both cases, you pressed the same key ("A"), so KeyDown/KeyUp will
give you the same NativeKeyCode. However, KeyPress events are about
characters (well, almost, Firefox and Opera are a bit different than
IE and Safari/Chrome), and you know that in the first case above
you'll have an "A" while the second case will generate an "a".
Need a more compelling difference? ever used the Alt+numeric key pad
combinations on Windows? try Alt+065 and Alt+097: three key
"gestures" (i.e. at least 3 key down + 3 key up events, I say at
least, because there might be keydown+keyup events for the Alt key)
but a single character being generated (keypress event); at least
that's the way it works in IE, and probably the future "standard
behavior" (those events are not spec'd anywhere yet), so expect
Firefox and others and update their behavior.

Ian Bambury

unread,
Aug 21, 2009, 7:01:00 PM8/21/09
to google-we...@googlegroups.com
You can use onKeyPress which will give you the character.

Key up/down gives you the key code not the character code. If you want the character, you have to amend the key code by checking the shift/alt gr/alt/ctrl and any other keys that might affect what you are trying to get. 

Reply all
Reply to author
Forward
0 new messages