简体   繁体   中英

Key Listener events not triggering?

I want to be able register a key pressed event that triggers a boolean variable. I've done it in my main class, but now I am trying to put it into classes, and it doesn't seem to be working.

Here is the hero class:

public class Hero extends Main {
private boolean downPressed;
private boolean leftPressed;
private boolean rightPressed;

public void init() {
}

public void paint(Graphics g, int x_pos, int y_pos) {

    if (isDownPressed() && isLeftPressed()) {
        this.sprite = hero225;
    } else if (isDownPressed() && isRightPressed()) {
        this.sprite = hero135;
    } else if (isUpPressed() && isRightPressed()) {
        this.sprite = hero45;
    } else if (isUpPressed() && isLeftPressed()) {
        this.sprite = hero315;
    } else if (isLeftPressed() == true) {
        this.sprite = hero270;
    } else if (isRightPressed() == true) {
        this.sprite = hero90;
    } else if (isUpPressed() == true) {
        this.sprite = hero;
        System.out.println("Succsess");
    } else if (isDownPressed() == true) {
        this.sprite = hero180;
    }

    // this.sprite will contain value set on last "movement"
    g.drawImage(this.sprite, x_pos, y_pos, this);
}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        setLeftPressed(true);
        System.out.println("keyPressed");
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        setRightPressed(true);

    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        setUpPressed(true);
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        setDownPressed(true);
    }

}

public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        setLeftPressed(false);
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

        setRightPressed(false);
    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        setUpPressed(false);

    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        setDownPressed(false);
    }
}

public void keyTyped(KeyEvent e) {

}

public void mouseClicked(MouseEvent e) {
    System.out.println("HIT!");

}

public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
    boolean isButtonPressed = true;

}

public void mouseReleased(MouseEvent e) {
    boolean isButtonPressed = false;

}


public void setDownPressed(boolean downPressed) {
    this.downPressed = downPressed;
}

public boolean isDownPressed() {
    return downPressed;
}

public void setLeftPressed(boolean leftPressed) {
    this.leftPressed = leftPressed;
}

public boolean isLeftPressed() {
    return leftPressed;
}

public void setRightPressed(boolean rightPressed) {
    this.rightPressed = rightPressed;
}

public boolean isRightPressed() {
    return rightPressed;
}

public void setUpPressed(boolean upPressed) {
    this.upPressed = upPressed;
}

public boolean isUpPressed() {
    return upPressed;
}

And here is the level class which calls it:

public class Level extends Main {
Hero hero = new Hero();

public void paint(KeyEvent e, Graphics g, double x_pos, double x_pos2) {
    repaint();
}

And here is the Paint function in the Main class which calls that:

public void paint(Graphics g) {
    Level level = new Level();
    level.paint(e, g, x_pos, y_pos);

The problem causing this doesn't seem to be apparent.

You should implement java.awt.event.KeyListener interface in the class(es) that you want to listen for events, in this case Hero, so:

public class Hero extends Main implements java.awt.event.KeyListener

and then register for the events in some method, maybe init or somewhere else using:

addKeyListener(this);
//rest of your code

Or if you want to listen for just some events, you could instead of implement the interface KeyListener, register an adapter:

addKeyListener(new java.awt.KeyAdapter()
{
   public void keyPressed(java.awt.KeyEvent e)
   {
       //handle just this event
   }
}
);
//rest of of your code

Sounds like a focus problem: Only the active component will get KeyEvents.

There are several ways to solve this problem, but I found this tutorial covers the ideas quite nicely. But if you want a quick and dirty (dirty? Not sure, easy and simple isn't bad I'd think; just gets a bit bloated for large projects) solution you could just implement a listener for every component and forward the events to some general class that handles it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM