Skip to content

Instantly share code, notes, and snippets.

@hoenn
Created July 31, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoenn/1ba7597fa4436f5b18a7 to your computer and use it in GitHub Desktop.
Save hoenn/1ba7597fa4436f5b18a7 to your computer and use it in GitHub Desktop.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener {
private Character lastKey;
//This current set up only registers keyPressed
public InputHandler(Game game)
{
game.addKeyListener(this);
lastKey=null;
}
@Override
public void keyPressed(KeyEvent e)
{
if((lastKey==null || lastKey!= e.getKeyChar()))
{
lastKey=e.getKeyChar();
applyInput(e.getKeyCode());
}
}
@Override
public void keyReleased(KeyEvent e)
{
lastKey=null;
}
@Override
public void keyTyped(KeyEvent e)
{
// TODO Auto-generated method stub
}
public void applyInput(int keyCode)
{
//This is where the input is actually handled
//Ex.1
//Create function calls to handle gameplay here
//abstract "gameplay functions" to a seperate class and
//call that here
//Ex.2
//Create a data structure to represent the keys pressed
//then view and call functions from another part of the code
if(KeyEvent.VK_LEFT==keyCode)
{
//Ex.1
//controller.moveCharacter(-1);
//Ex.2
//keys[0]=1;
}
if(KeyEvent.VK_RIGHT==keyCode)
{
}
if(KeyEvent.VK_UP==keyCode)
{
}
if(KeyEvent.VK_DOWN==keyCode)
{
}
if(KeyEvent.VK_ENTER==keyCode)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment