views:

43

answers:

2

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.

+1  A: 

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.

G. Davids
+1  A: 

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.

Chris Smith