Clearout Clearout - 1 year ago 62
Java Question

Why won't the keyPressed method be called?

I'm making a fighting game in Java for a project and trying to get a picture to move and be repainted across the panel reacting to the keyboard (keyEvents). I'm trying to accomplish this by having a switch in the keyPressed method, while adding the keyListener to the panel. I have been following an example in my Java book and the code I've written as nearly the same yet it just won't work.

What I'm really wondering is why it doesnt seem to react to keyEvents at all. The program compiles fine and all, yet nothing happens. I have no idea what is going wrong. It doesn't reach the breakpoint in the

method if I make one, nor will it it do a
if I put it in there. So the
method doesnt react at all. I have also tested and made sure that the panel is focusable so I'm sure it has the keyboard focus.

public class MovePanel extends JPanel implements KeyListener {
private ImageIcon currentImage, facingLeft, facingRight;
private int position;
private final int MOVEMENT;
private GameFrame gameFrame;
private URL lefturl, righturl;

public MovePanel(GameFrame gameFrame) {
// Taking in a gameFrame to be able to swap the active panel
// (not really relevant).
this.gameFrame = gameFrame;

// Adding the key listener here.

// These are just the Images I'm using to test.
// Trying to get it to swap from one to the other.
lefturl = getClass().getResource("/Images/facingLeft.jpg");
righturl = getClass().getResource("/Images/facingRight.jpg");

facingLeft = new ImageIcon(lefturl);
facingRight = new ImageIcon(righturl);

currentImage = facingLeft;
position = 50;

setPreferredSize(new Dimension(600,300));

// Calling this method so that the panel will react
// to the keyboard without having to be clicked.

// This is just the paintComponent method which works fine to paint the image
// when starting the game.
public void paintComponent(Graphics page) {
currentImage.paintIcon(this, page, position, 170);

// No matter what I try to do inside the keyPressed method
// it doesnt seem to react at all.
public void keyPressed(KeyEvent e) {

// This switch is to make the method react accordingly to the keys pressed.
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:

// Here I'm changing the "active" image and the position
// by changing the position variable which is used
// to determine the x placement of the image.
// This case is suppused to react if the left arrow key is pressed.
currentImage = facingRight;
position -= MOVEMENT;
case KeyEvent.VK_RIGHT:
currentImage = facingRight;
position += MOVEMENT;

// This case is to exit to the menu when escape is pressed.
case KeyEvent.VK_ESCAPE:
// After reacting to any of the proper keys pressed
// I'm trying to repaint which will use the
// paintComponent method to paint the new image in its new position.
// I have empty definitions for the other
// implemented methods but won't be posting them.

Does anyone have any idea why this doesn't work? Why
method won't react?

Answer Source

I dont see the code like below

you should call the below line where u create the instance of MovePanel

 MovePanel.requestFocus();      // Give the panel focus.

public class demo extends JFrame 
    MovePanel  panel;

    public demo () 
        panel= new MovingTextPanel();
        this.getContentPane().setLayout(new BorderLayout())
        panel.requestFocus();      // Give the panel focus.

In your MovePanel add setFocusable to true

 public MovePanel(GameFrame gameFrame) {

        this.setFocusable(true);   // Allow this panel to get focus.
        // Adding the key listener here.

Some more traces

- Characters (a, A, #, ...) - handled in the keyTyped() listener.
- Virtual keys (arrow keys, function keys, etc) - handled with keyPressed() listener.
- Modifier keys (shift, alt, control, ...) - Usually their status (up/down) is tested by calls in one of the other listeners, rather than in keyPressed().

  public void keyTyped(KeyEvent e) {
  public void keyPressed(KeyEvent e) {
  public void keyReleased(KeyEvent e) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download