I have the following class implemented in order to better control keyboard input...
Where update() is called in the World's act() method. The purpose of keyHit() is to ensure that key input is only registered once per press.
For example. If I call
in my game loop the statement will be printed if I press enter and will not be reprinted until I release the key and press it again.
Unfortunately the code only partially works. If I press and hold "enter", the statement is printed and after a brief delay input is delivered at the update rate. I can't figure out what's going on. Can anyone help?
import greenfoot.*;
public class KeyboardInput
{
String currentKey = null;
String previousKey = null;
public void update()
{
previousKey = currentKey;
currentKey = Greenfoot.getKey();
}
public boolean keyHit(String key)
{
return key.equals(currentKey) && !key.equals(previousKey);
}
}if(keyboardInputObject.keyHit("enter")) System.out.println("enter hit");
