lalit
lalit

Reputation: 1475

GWT native event does not provide intended behavior

I have a text widget where I want to make enter event to behave like a tab event. so I capture the Key press event and raise a tab native event.

However the tab behavior is not reflected in the application. The code for event handler is

   public void onKeyPress(KeyPressEvent event) { 
            int keyCode = event.getNativeEvent().getKeyCode(); 
            if (keyCode == KeyCodes.KEY_ENTER) { 
               NativeEvent nativeEvent = 
               Document.get().
                 createKeyPressEvent(false,false,false,false,KeyCodes.KEY_TAB ); 
               DomEvent.fireNativeEvent(nativeEvent, this, this.getElement()); 
            } 

When I use the deprecated createKeyPressEvent with more argument, it fires the tab event but the behavior is not as per the tab key press, which is to move to next widget. The new code changes from the above code in createKeyPress event line as follows

     NativeEvent nativeEvent = 
       Document.get().
         createKeyPressEvent(false,false,false,false,
                      KeyCodes.KEY_TAB ,KeyCodes.KEY_TAB);

Upvotes: 2

Views: 1723

Answers (1)

z00bs
z00bs

Reputation: 7498

Note that manually firing an event does not generate the default action associated with that event. For example, manually firing a focus event does not cause the element to receive focus (you must use its focus method for that), manually firing a submit event does not submit a form (use the submit method), manually firing a key event does not cause that letter to appear in a focused text input, and manually firing a click event on a link does not cause the link to be activated, etc. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.

From http://www.howtocreate.co.uk/tutorials/javascript/domevents.

Upvotes: 1

Related Questions