Home » Java » java – How to set up a key press detection that toggles the state of the boolean variable-Exceptionshub

java – How to set up a key press detection that toggles the state of the boolean variable-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


How to set up a key press detection that toggles the state of the boolean variable paused where paused is an attribute of my GCTester class with an initial state of false?

// GCTester demonstrates how we can override the GameCore class
// to create our own 'game'. We usually need to implement at
// least 'draw' and 'update' (not including any local event handling)
// to begin the process. You should also write your own 'init'
// method that will initialise event handlers etc. By default GameCore
// will handle the 'Escape' key to quit the game but you should
// override this with your own event handler.

public class GCTester extends GameCore {

    private Animation anim;
    private Sprite rock;
    long total;         // Total time elapsed
    boolean paused;

    // The obligatory main method that creates
    // an instance of our GCTester class and
    // starts it running
    public static void main(String[] args) {
        GCTester gct = new GCTester();
        // Start in windowed mode with a 800x600 screen
        gct.run(false, 800, 600);

    // Initialise the class, e.g. set up variables
    // animations, register event handlers
    public void init() {
        total = 0;
        Image player = loadImage("images/rock.png");
        anim = new Animation();
        anim.addFrame(player, 20);

        rock = new Sprite(anim);


    // Draw the current frame
    public void draw(Graphics2D g) {
        // A simple demo - note that this is not
        // very efficient since we fill the screen
        // on every frame.
        g.fillRect(0, 0, 800, 600);
        g.drawString("Time Expired:" + total, 30, 50);


    // Update any sprites and check for collisions
    public void update(long elapsed) {
        if (paused) return;
        total += elapsed;
        if (total > 60000) stop();

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_ESCAPE)
            stop(); // Call GameCore stop method to stop the game loop
        else {
            if (keyCode == KeyEvent.VK_S) rock.stop();
            if (keyCode == KeyEvent.VK_G) rock.setVelocityX(0.1f);

How to&Answers:

You should use a window component to show your app on screen. Suppose it’s a JFrame (or it can be a JDialog). Create a JComponent that will ‘listen’ to key actions, add it to your jFrame, or any other upper window component you use:

final JComponent CONTROL = new JComponent() {};
CONTROL.setBounds(0, 0, 800, 600); // your bounds

ActionListener keyEscListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
        paused = !paused;
        // your logic for Escape

// tie KeyStroke and keyboard action to the CONTROL
KeyStroke keyEsc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
CONTROL.registerKeyboardAction(keyEscListener, keyEsc, JComponent.WHEN_FOCUSED);

This method requires creating separate Listener and KeyStroke object for each key you want to listen