framework
Class KeyboardState

java.lang.Object
  extended by framework.KeyboardState
All Implemented Interfaces:
KeyListener, EventListener

public class KeyboardState
extends Object
implements KeyListener

Listens to KeyEvent events to build a model of the state of the keyboard.

The distinction between isPressed, isCurrentlyPressed and wasTyped is not immediately obvious. In short, if you want to do something as long as a key is held down, use isPressed. If you want to do something once for every time the key is pressed, use wasTyped.

Example usage:

 import java.awt.event.KeyEvent;
 
 public class KeyboardDemo extends Game
 {
        private KeyboardState keyboard = new KeyboardState();
        
        public void init ()
        {
                addKeyListener(keyboard);
        }
        
        public void update (int time)
        {
                keyboard.update();
                
                if (keyboard.isPressed(KeyEvent.VK_LEFT))
                {
                        ...
                }
        }
 }
 


Constructor Summary
KeyboardState()
           
 
Method Summary
 boolean isCurrentlyPressed(int key)
           
 boolean isPressed(int key)
           
 void keyPressed(KeyEvent e)
          Required for KeyListener interface.
 void keyReleased(KeyEvent e)
          Required for KeyListener interface.
 void keyTyped(KeyEvent e)
          Required for KeyListener interface.
 void update()
          Process events which have been received since the last call to this method.
 boolean wasTyped(char c)
           
 boolean wasTyped(int key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyboardState

public KeyboardState()
Method Detail

update

public void update()
Process events which have been received since the last call to this method. This method should be called once per game loop.


isPressed

public boolean isPressed(int key)

isCurrentlyPressed

public boolean isCurrentlyPressed(int key)

wasTyped

public boolean wasTyped(int key)

wasTyped

public boolean wasTyped(char c)

keyPressed

public void keyPressed(KeyEvent e)
Required for KeyListener interface.

Specified by:
keyPressed in interface KeyListener

keyReleased

public void keyReleased(KeyEvent e)
Required for KeyListener interface.

Specified by:
keyReleased in interface KeyListener

keyTyped

public void keyTyped(KeyEvent e)
Required for KeyListener interface.

Specified by:
keyTyped in interface KeyListener