Advertisement

How To Get Keyboard Input - In Simple English ?

Started by March 06, 2014 04:23 PM
1 comment, last by RLS0812 10 years, 11 months ago

I am having trouble getting any kind of input from KeyListener - and can not find any good examples around.

Could some one post a working, simplified & bare bones example on how to get this to work ?

Here is my version - which does nothing useful .

Game Loop


public class GameLoop {
KeyBoard kb;
boolean RUN;

     public GameLoop(){
     RUN = true;
     kb = new KeyBoard();

    while(RUN){
          System.out.println(kb.getX() );
          //dostuff();
          }
     }
}
	
 

KeyBoard


public class KeyBoard implements KeyListener{
int x = 0 ;
	public void keyPressed(KeyEvent e) {}
	public void keyReleased(KeyEvent e) { x ++;} 
	public void keyTyped(KeyEvent e) {}
	
public String getX(){return "" + x ;}
}

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-keyboard-and-mouse-r2439

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Advertisement

There is a reason I said "Simplified" and "Bare Bones" .

Linking to two articles on theory with overly complex and mostly irrelevant examples DO NOT HELP.

I figured out why the code I posted does not work - it can be summed up in less than 10 words .

"Requires: extends JFrame that isFocusable(true) and has addKeyListener(this) envoked"

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

This topic is closed to new replies.

Advertisement