Hello all,
Here's some very straightforward code to capture keyboard events. It
works find in hosted mode (and IE), however in mozilla 3.0 it seems to
not respond to keyboard events..
-----------------------------------------------------------------------------------------------------
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextBox;
public class Workbench extends VerticalPanel implements EntryPoint {
TextBox tb;
public void onModuleLoad() {
tb = new TextBox();
add(tb);
setSize("200px", "200px");
DOM.setStyleAttribute(this.getElement(), "background", "red");
sinkEvents(Event.KEYEVENTS);
RootPanel.get().add(this);
}
public void onBrowserEvent(Event e) {
int type = DOM.eventGetType(e);
Element el = DOM.eventGetFromElement(e);
if ( type == Event.ONKEYDOWN ) {
if ( KeyboardListener.KEY_CTRL == DOM.eventGetKeyCode(e) ) {
tb.setText("Ctrl Pressed");
}
if ( KeyboardListener.KEY_SHIFT == DOM.eventGetKeyCode(e) ) {
tb.setText("Shift Pressed");
}
}
if ( type == Event.ONKEYUP ) {
if ( KeyboardListener.KEY_CTRL == DOM.eventGetKeyCode(e) ) {
tb.setText("Ctrl Left");
}
if ( KeyboardListener.KEY_SHIFT == DOM.eventGetKeyCode(e) ) {
tb.setText("Shift Left");
}
}
}
}