tags:

views:

3536

answers:

3

Hi

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

+1  A: 

Maybe ClickListenerCollection.fireClick() does what you want. I haven't used it though

Sorry, that is deprecated and maybe not even useful. I guess you shoul look at widget.delegateEvent instead.

HerdplattenToni
+6  A: 

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 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)
DLH
Alexander Vasiljev
+2  A: 

The method described by DLH should work and except for the detail argument (which I have no idea what's it for) you have the other arguments available in the KeyPressEvent.

Another possible solution is to call the native JavaScript click() on the element. I've done this in a Button widget (Which is available as open source). See click() method in the following class: http://code.google.com/p/cobogw/source/browse/trunk/widgets/src/main/java/org/cobogw/gwt/user/client/ui/Button.java), which calls a specific Event2 class, that implements the browser specific versions of the click method.

To use this method, you could simply add the jar file provided with cobogw to your project, include the Event.gwt.xml and call Event2.fireClickEvent(getElement()); in your method or only use the code from the classes Event2 and Event in your own project

This solution also allows you the programmatically fire a click event.

Also take a look at the onBrowserEvent implementation in the Button class mentioned above , since it handles the key event in a similar way you want, and works around the problem of the firing of multiple key events, when you only want to generate 1 click event.

Hilbrand