|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectframework.KeyboardState
public class KeyboardState
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 |
---|
public KeyboardState()
Method Detail |
---|
public void update()
public boolean isPressed(int key)
public boolean isCurrentlyPressed(int key)
public boolean wasTyped(int key)
public boolean wasTyped(char c)
public void keyPressed(KeyEvent e)
KeyListener
interface.
keyPressed
in interface KeyListener
public void keyReleased(KeyEvent e)
KeyListener
interface.
keyReleased
in interface KeyListener
public void keyTyped(KeyEvent e)
KeyListener
interface.
keyTyped
in interface KeyListener
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |