TextBox with onKeyPress listener doesn't work

Skip to first unread message


May 19, 2006, 10:57:58 AM5/19/06
to Google Web Toolkit
I tried the following simple thing almos right from the documentation
and it didn't work.
In the simulator I get a text field that doesn't accept any input at
In Firefox I get no text field showing at all.
If I remove the onKeyPress method it works fine.
I use Java 1.5.0_06.
Any ideas on what goes wrong?

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;

public class Tryit implements EntryPoint {

public void onModuleLoad() {
final Label label = new Label();
final TextBox text = new TextBox();

// Let's disallow non-numeric entry in the normal text box.
text.addKeyboardListener(new KeyboardListenerAdapter() {
public void onKeyPress(Widget sender, char keyCode, int
modifiers) {
if (!Character.isDigit(keyCode)) {
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
if (keyCode == KeyboardListener.KEY_ENTER) {



May 19, 2006, 11:23:44 AM5/19/06
to Google Web Toolkit

Your code works on Both on the simulator and IE. Since simulator is
based IE in case of Windows version of GWT ( -- it is using SWT for
this purpose). Issue is with Firefox. I think this a bug in GWT.



May 19, 2006, 11:36:02 AM5/19/06
to Google Web Toolkit
Forgot to mention that I was running on Linux which can explain the
difference in the simulator behaviour.


May 19, 2006, 3:42:11 PM5/19/06
to Google Web Toolkit
I want to confirm this bug, with a few more details:

KeyboardListener.onKeyPress(Widget sender, char keyCode, int modifiers)
does not work properly under Firefox Windows.

More specifically the argument keyCode is always >>0<< no matter what
key is pressed under Firefox.

Under Internet Exporer 6.0.2800 things work fine.


May 20, 2006, 9:25:48 PM5/20/06
to Google Web Toolkit
The problem is that GWT does not handle the the way that
Mozilla/Firefox uses keyCode. For a key press event, evt.keyCode is
only set for the non-character codes (Function keys, PageUp etc). For
character keys the evt.charCode field is set. For key up/down
events evt.charCode is never set, only evt.keyCode. There is another
variable evt.which which is set in all cases (I think :)

In com/google/gwt/user/client/impl/DOMImpl.java we have:
public native char eventGetKeyCode(Event evt) /*-{
return evt.keyCode;

For Mozilla this should either be
return evt.which;
or the shotgun approach
return (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode
: ((evt.which) ? evt.which:0));

In the compiled code for my test app I found the line
function az(bz){return bz.keyCode;}
and changed it to
function az(bz){return bz.which;}
and the key filters started to work in Firefox.



May 26, 2006, 8:29:37 AM5/26/06
to Google Web Toolkit
This bug still exists in GWT 1.0.21. Bret, can you add this to the list
of issues?




May 31, 2006, 5:14:15 PM5/31/06
to Google Web Toolkit
A bit late on this one, but I'm adding it to the issues list.



Jun 6, 2006, 1:41:26 PM6/6/06
to Google Web Toolkit
Can we get an update on the status of this bug? I don't see it in the
public issue tracking list...


Jun 12, 2006, 9:23:47 PM6/12/06
to Google Web Toolkit
this problem still at gwt-windows-1.0.21 run on firefox1.5.0.4 apears.


Jun 15, 2006, 6:16:09 AM6/15/06
to Google Web Toolkit
kong.l...@gmail.com wrote:
> this problem still at gwt-windows-1.0.21 run on firefox1.5.0.4 apears.

Yes, a very nasty one. I could not find a workaround, the methdods in
the class could not be overridden (I admit that my Java is a bit rusty
now, though).

Has anyone found a simple workaround yet?


Jun 15, 2006, 8:07:37 AM6/15/06
to Google Web Toolkit
mkn wrote:
> Has anyone found a simple workaround yet?

Answering to myself. I did not understand that the .jar files had the
source code included. It was a trivial task to change the method to an
abstract one:

public abstract char eventGetKeyCode(Event evt);

and add the correct mutations in the browser implementations. Then
simply compile and re-jar the stuff and that was it.


Jun 22, 2006, 11:17:34 AM6/22/06
to Google Web Toolkit
Would you mind going into a little more detail on this? I have run
into the same issue, but I dont really understand what you mean by "add
the correct mutations in the browser implementations" Thanks for any


Jun 26, 2006, 3:21:49 AM6/26/06
to Google Web Toolkit
Okay, this is what I did:

1) Unpack the gwt-user.jar (dropping it to winzip or using any tool
that can cope with .jar files). My gwt is installed in
h:\java\gwt-windows-1.0.21 and the example path below reflects the

change directory to [your_gwt_root]\com\google\gwt\user\client\impl

In my case: H:\java\gwt-windows-1.0.21\com\google\gwt\user\client\impl

change the declaration and the body of eventGetKeyCode() method from
the original

public native char eventGetKeyCode(Event evt) /*-{
return evt.keyCode;


public abstract char eventGetKeyCode(Event evt);

So no function body here, and because the method is ABSTRACT it MUST be
implemented in the browser specific sections. Now take the file
DOMImpIE6 and add this (it should be exactly the same that you just
replaced in the previous file:

public native char eventGetKeyCode(Event evt) /*-{
return evt.keyCode;

So this is the IMPLEMENTATION of the abstract method that we just
defined in the base class. Add the same method to all other
DOMImplWhatEverBrowser.java files with one exception: In
DOMImplMozilla you must add something like this:

public native char eventGetKeyCode(Event evt) /*-{

return (evt.charCode) ? evt.charCode : ((evt.keyCode) ?
evt.keyCode: ((evt.which) ? evt.which:0));


So this is the 'mutation' for Mozilla. The rest of the browser
implementations will probably work with the original. Oh, add the
first method to DOMImplStandard.java as well.

Now you must compile the java files you modified and re-jar the stuff
to the jar package you started from.

Hope this helps....


Jun 26, 2006, 12:28:59 PM6/26/06
to Google Web Toolkit
Yes that is extremely helpful, thanks for the detailed explanation.


Jun 27, 2006, 3:59:05 PM6/27/06
to Google Web Toolkit
Actually I have one more question, how did you compile it? I tried
compiling it just on the command line using javac, and I tried
importing the filesystem gwt-user into eclipse. Both of these methods
throws lots of compile errors. It is as if it is looking for some
other files or libraries, etc. Thanks for any help.


Jun 28, 2006, 8:03:08 AM6/28/06
to Google Web Toolkit
ela...@gmail.com wrote:
> Actually I have one more question, how did you compile it? I tried
> compiling it just on the command line using javac, and I tried

Well... well... a quick-and dirty way is to go to the directory where
the modified .java files are and compile them from there. BUT: you MUST
give the correct classpath:

In my case GWT was installed in
and the modified files in


I was working in the modified directory and gave this command (for

javac -cp h:\java\gwt-windows-1.0.21 DOMImplMozilla.java

So the compiler finds the correct classes (com/google... etc)

In general, you must compile it:

javac -cp [your gwt-dir] [the-file-to-compile]

Hope it works this way in your case as well.



Jun 30, 2006, 5:13:48 PM6/30/06
to Google Web Toolkit
it works. thanks


Jul 3, 2006, 3:50:44 AM7/3/06
to Google Web Toolkit
> javac -cp h:\java\gwt-windows-1.0.21 DOMImplMozilla.java

Just one more side note: The correct way is, of course, to change the
CLASSPATH environment variable either permanently (adding the the GWT
root to to it in the Control Panel) or the old-fashioned way in the
command shell (which I prefer for some reason) using a batch file, say

@echo off
SET CLASSPATH=%CLASSPATH%;h:\java\gwt-windows-1.0.21

and running this in the command shell. When working in the command
shell, doing small changes I tend to use this approach. Simple, handy
and _very_ portable :)

Reply all
Reply to author
0 new messages