JTP
Class KeyHandler

java.lang.Object
  extended by JTP.KeyHandler
All Implemented Interfaces:
KeyListener, EventListener

public class KeyHandler
extends Object
implements KeyListener

Class for handling keyboard control in-game.

Author:
V.Termanis

Field Summary
static int GAME_1P
          Handling mode: Only Player 1 keys allowed
static int GAME_2P
          Handling mode: Both Player keys allowed
static int NORMAL
          Handling mode: Bypassed (handling performed by Screen class)
 
Constructor Summary
KeyHandler(Screen s)
          Constructs a new instance of KeyHandler.
 
Method Summary
 void autoStart()
          To be called at start of level (to perform automatic moves, if any)
 void enableSpaceKey()
          To be called when a popup message is shown in-game so it can be cancelled via keyboard.
 boolean keyDown()
          Used by Screen class to determine whether a key is being held down in-game.
 void keyPressed(KeyEvent e)
          Handles key-down events.
 void keyReleased(KeyEvent e)
          Handles key-up events.
 void keyTyped(KeyEvent e)
          Handles key-typed events.
 void reset()
          Resets state of this KeyHandler instance.
 void setMode(int mode, boolean repeatsAllowed)
          Sets key input handling mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NORMAL

public static final int NORMAL
Handling mode: Bypassed (handling performed by Screen class)

See Also:
Constant Field Values

GAME_1P

public static final int GAME_1P
Handling mode: Only Player 1 keys allowed

See Also:
Constant Field Values

GAME_2P

public static final int GAME_2P
Handling mode: Both Player keys allowed

See Also:
Constant Field Values
Constructor Detail

KeyHandler

public KeyHandler(Screen s)
Constructs a new instance of KeyHandler.

Parameters:
s - the parent processing class.
Method Detail

setMode

public void setMode(int mode,
                    boolean repeatsAllowed)
             throws IllegalArgumentException
Sets key input handling mode.

Parameters:
mode - the mode to use. Available modes:
  • NORMAL - Simply forwards all events to the Screen class
  • GAME_1P - Handling of 1st player keys only
  • GAME_2P - Handling of both players' keys
repeatsAllowed - indicates whether holding down a key should produce multiple events or not. Note: This parameter has no effect in NORMAL mode.
Throws:
IllegalArgumentException - if mode is not one of NORMAL, GAME_1P or GAME_2P.

reset

public void reset()
Resets state of this KeyHandler instance. Note: The repeatsAllowed flag is not reset.


autoStart

public void autoStart()
To be called at start of level (to perform automatic moves, if any)


enableSpaceKey

public void enableSpaceKey()
To be called when a popup message is shown in-game so it can be cancelled via keyboard.


keyPressed

public void keyPressed(KeyEvent e)
Handles key-down events.

Specified by:
keyPressed in interface KeyListener
Parameters:
e - the incoming keyboard event.

keyReleased

public void keyReleased(KeyEvent e)
Handles key-up events.

Specified by:
keyReleased in interface KeyListener
Parameters:
e - the incoming keyboard event.

keyDown

public boolean keyDown()
Used by Screen class to determine whether a key is being held down in-game.

Returns:
true is a player or control key is in the down state, given the current key handling mode. Note: In NORMAL mode this will always return false.

keyTyped

public void keyTyped(KeyEvent e)
Handles key-typed events. Note: This event is always bypassed (i.e. handled by Scene class because it is only used when entering characters as part of a new highscore.

Specified by:
keyTyped in interface KeyListener
Parameters:
e - the incoming keyboard event.