I have created a custom widget in gwt which extends the composite.I am using focus panel in that.For FocusPanel I added ClickHandler.Then I have added keyboard listner.Now on press of Enter key it should trigger click event.Can any one help me to trigger click event by using code in GWT?
focusPanel.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
// TODO call onClick() method
}
}
});
Thanks
I have done this code:
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
myButton.fireEvent( new GwtEvent<ClickHandler>() {
@Override
public com.google.gwt.event.shared.GwtEvent.Type<ClickHandler> getAssociatedType() {
return ClickEvent.getType();
}
@Override
protected void dispatch(ClickHandler handler) {
handler.onClick(null);
}
});
}
Of course myButton must be final or public cause you are inside another event handler.
I haven't done this for a click event, but I've done change events like this.
NativeEvent event = Document.get().createChangeEvent();
DomEvent.fireNativeEvent(event, this);
The [createClickEvent
](http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/dom/client/Document.html#createClickEvent(int,%20int,%20int,%20int,%20int,%20boolean,%20boolean,%20boolean,%20boolean)) method takes a lot more parameters though.
public final NativeEvent createClickEvent(int detail,
int screenX,
int screenY,
int clientX,
int clientY,
boolean ctrlKey,
boolean altKey,
boolean shiftKey,
boolean metaKey)
You can also use a simple JSNI method to do it. Just pass your element [e.g. button.getElement()] to this method:
public static native void clickElement(Element elem) /*-{
elem.click();
}-*/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With