Java applet game design no keyboard focus

Posted by Sri Harsha Chilakapati on Game Development See other posts from Game Development or by Sri Harsha Chilakapati
Published on 2012-08-19T00:53:46Z Indexed on 2012/08/27 21:58 UTC
Read the original article Hit count: 281

Filed under:
|
|

THIS IS PROBABLY THE WRONG PLACE. POSTED ITHERE (STACKOVERFLOW)

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
        setFocusable(true);
        requestFocus();
        requestFocusInWindow();  // Always returning false
        GInput.install(this);    // Install the input manager for this class
        new Thread(this).start();
    }

    public void run(){
        startGameLoop();
    }

}

And Here's the GInput class.

public class GInput implements KeyListener {

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

    public GInput(Component c){
        c.addKeyListener(this);
    }

    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.

Thanks

© Game Development or respective owner

Related posts about java

Related posts about architecture