Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


I'm making an applet game and it is rendering, the game loop is running, the animations are updating, but the keyboard input is not working. Here's an SSCCE.

public class Game extends JApplet implements Runnable {

    public void init(){
        // Initialize the game when called by browser
        requestFocusInWindow();  // Always returning false
        GInput.install(this);    // Install the input manager for this class
        new Thread(this).start();

    public void run(){


And Here's the GInput class.

public class GInput implements KeyListener {

    public static void install(Component c){
        new GInput(c);

    public GInput(Component c){

    public void keyPressed(KeyEvent e){
        System.out.println("A key has been pressed");



This is my GInput class. When run as an applet, it doesn't work and when I add the Game class to a frame, it works properly.


share|improve this question

Try overriding Component.processKeyEvent in the Applet class. Also you may need to call enableEvents(AWTEvent.KEY_EVENT_MASK); in the initialization. I have not tested this with Applets, but generally this has been the most robust way of handling keyboard in Java that I am aware of.

share|improve this answer
Sorry but I've found a solution for this in stack overflow. I've also posted the link in the post – Sri Harsha Chilakapati Aug 28 '12 at 1:02
It would still be nice to know if this solution worked for the question you asked – msell Aug 28 '12 at 5:25

Specify what key should be pressed to print your message.

public void keyPressed(KeyEvent e){
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_A)
       System.out.println("A key has been pressed");
share|improve this answer
But it's not working for any key. I wan't to print it for any key – Sri Harsha Chilakapati Aug 20 '12 at 1:20
"A key has been pressed" ha. – API-Beast Aug 27 '12 at 11:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.