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)) {
((TextBox)sender).cancelKey();
}
}
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
if (keyCode == KeyboardListener.KEY_ENTER) {
label.setText(((TextBox)text).getText());
}
}
});
RootPanel.get("slot0").add(text);
RootPanel.get("slot1").add(label);
}
}
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.
Regards,
Mohan
KeyboardListener.onKeyPress(Widget sender, char keyCode, int modifiers)
does not work properly under Firefox 1.5.0.3 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.
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.
enjoy
scott
Thanks,
Rob
joel.
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?
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.
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
situation.
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;
}-*/;
to
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....
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
H:\java\gwt-windows-1.0.21
and the modified files in
H:\java\gwt-windows-1.0.21\com\google\gwt\user\client\impl
I was working in the modified directory and gave this command (for
Mozilla):
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.
Markku
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
GWT_CP.BAT:
@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 :)