How To Simulate KeyPress

2,780 views
Skip to first unread message

Daniel Simons

unread,
May 4, 2010, 10:12:28 AM5/4/10
to Google Web Toolkit
I am creating a mobile web app with GWT and have discovered that the TextBox.setFocus(true) method does not work in mobile Safari.  I also tried using JSNI to directly call element.focus(), which also did not work.  Looking at other potential work arounds, the textbox in my application that I would like to focus on happens to be the input immediately following the input for which the event is being triggered.  Therefore, if I could somehow simulate the pressing of the "TAB" key inside that event, I could achieve a similar result to "setFocus(true)".  Could anyone explain how I could go about simulating the TAB Key press?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Lukas Laag

unread,
May 4, 2010, 3:43:35 PM5/4/10
to Google Web Toolkit
It is possible to generate events programmatically in JavaScript. This
page gives a good overview of the topic, including key events (look
for 'Manually firing events'):

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

I do not know if generating a tab event like this in a JSNI method
would solve your focus problem. If you give it a try, can you please
let us know how this fares ?

Lukas

Thomas Broyer

unread,
May 5, 2010, 11:01:39 AM5/5/10
to Google Web Toolkit


On May 4, 9:43 pm, Lukas Laag <laa...@gmail.com> wrote:
> It is possible to generate events programmatically in JavaScript. This
> page gives a good overview of the topic, including key events (look
> for 'Manually firing events'):
>
> http://www.howtocreate.co.uk/tutorials/javascript/domevents.
>
> I do not know if generating a tab event like this in a JSNI method

you actually don't need JSNI: just use Document.get().createXxxEvent()
and DomEvent.fireNativeEvent().

> would solve your focus problem.

I doubt it would, otherwise people would have proposed it as a
workaround:
http://stackoverflow.com/questions/2398528/set-textbox-focus-in-mobile-safari

(note: according to "the web", it's a bug in WebKit)

Lex

unread,
Jun 14, 2010, 4:13:29 PM6/14/10
to Google Web Toolkit
I have a question related to this.

I have a KeyPressHandler in the TextBox. When the user presses
"Enter", I want to simulate it as "Tab".

I have added the necessary code to capture the 'Enter" event, and then
I fire the new event using the DomEvent.fireNativeEvent.

NativeEvent tabEvent = Document.get().createKeyPressEvent(false,
false, false, false, 9, 9);
DomEvent.fireNativeEvent(tabEvent, textBox);


What I am hoping to see is the 'tabbing' of the current cursor to the
next Focusable widget, but it doesn't seem to happen.

What am I missing?

It basically re-enteres the KeyPressHandler since this is the widget
that is being fired. In short, I need the way to suppress the current
KeyPressHandler and have the Browser level KeyPressHandler of the
'tabbing' to occur.

Any help is appreciated.

Regards


On May 5, 11:01 am, Thomas Broyer <t.bro...@gmail.com> wrote:
> On May 4, 9:43 pm, Lukas Laag <laa...@gmail.com> wrote:
>
> > It is possible to generate events programmatically in JavaScript. This
> > page gives a good overview of the topic, includingkeyevents (look
> > for 'Manually firing events'):
>
> >http://www.howtocreate.co.uk/tutorials/javascript/domevents.
>
> > I do not know if generating atabevent like this in a JSNI method
>
> you actually don't need JSNI: just use Document.get().createXxxEvent()
> and DomEvent.fireNativeEvent().
>
> > would solve your focus problem.
>
> I doubt it would, otherwise people would have proposed it as a
> workaround:http://stackoverflow.com/questions/2398528/set-textbox-focus-in-mobil...

Sky

unread,
Jun 14, 2010, 9:50:26 PM6/14/10
to Google Web Toolkit
Lex, here's a thought: can you determine which Widget or DOM element
that you want to gain focus when the user hits ENTER? If you can,
don't bother creating that event, just talk directly to the widget or
the DOM element through JSNI and give it focus. I don't know why your
solution doesn't work, but I think setting the focus on something is
the better approach. Not sure though for your specific situation.

gl

Se Hee Lee

unread,
Jun 14, 2010, 10:48:17 PM6/14/10
to google-we...@googlegroups.com
Thanks for your input.

Yes. I know such solution works, but since the order of the widget isn't consistent and changes every time the screen is rendered, I need to perform some calculation to figure out the next widget with higher tab index to get focus. 

I was hoping that by firing a key press event with the tab, it would naturally position to the Focusable widget with next tab index automatically, in much simpler and concise manner.

--
"Everything should be made as simple as possible, but not simpler."

- Albert Einstein

Sky

unread,
Jun 14, 2010, 11:21:35 PM6/14/10
to Google Web Toolkit
I can understand that. I am going to hazard a guess that firing an
event and setting the value of the keycode to the TAB key likely is
only useful for any code that Handles such a keystroke... it won't
actually invoke the Tab keypress event in and of itself. Course, this
is just a guess, I don't know and I'm not going to spend the time
right now to try to do it myself. It is interesting and I would like
to know the truth myself, it just sucks how time is so rare, I wish I
had a lot more of it :(

Hopefully you can make the design so it can nicely keep track of what
widgets are where or just do a search to find the next one. I'm sure
you can do it without too much work.

gl :)
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/google-web-toolkit?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages