Xavier
Xavier

Reputation: 9049

In GWT, how do I get a keypress?

I'm making a game and would like to know how I get a keypress event. All the examples I have seen are using textboxes to register a key handler but I don't think I need to do that. I just want to get key events for up,down,left,right to move a character.

Upvotes: 2

Views: 558

Answers (2)

Chris Smith
Chris Smith

Reputation: 18712

Yes, to expand upon what G. Davis said, in GWT to receive general input events (such as from the Mouse or Keyboard) you put your game content inside of a FocusPanel. That object then will fire any input events which occur within that panel. (So you should have all of your game content as a child of the parent FocusPanel.)

A gotcha worth pointing out is that non-printable characters, such as KEY_LEFT or KEY_ESCAPE, cannot be caught via the onKeyPressed event and can only reliably be caught with the onKeyDown event. See KeyboardListener.

Upvotes: 3

Gerhard Davids
Gerhard Davids

Reputation: 485

You can wrap whatever you are using for your display in a FocusPanel. FocusPanel's can pick up on keyboard and mouse events. Just add the handlers as needed.

Upvotes: 3

Related Questions