Search code examples
javaswingevent-handlingjpanelkeylistener

KeyListener not responding in Java swings?


I'm trying to make some simple game when i press the key up or key down i want to move my rectangle, but the problem is keylistener is not working..

I've made separate class for keylistener events called "classKeyEvents" is this causing problem or the way i'm adding keylistener to the container is causing the problem

My Code

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Pong extends JPanel
{
    static public JFrame frame = null;
    Container con  = null;

    public static int paddleX = 10, paddleY = 270, paddleWidth = 10, paddleHeight = 80;
    public static int upSpeed = 5;
    public static int downSpeed = 5;


    public void creatWindow()
    {
        frame = new JFrame();
        con = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(200,50,900,600);
        frame.setVisible(true);

        con.add(this);
        con.setFocusable(true);
        con.addKeyListener(new classKeyEvents());

        Runnable ml = new mainLoop();
        Thread th = new Thread(ml);
        th.start();

    }


    protected void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      g.setColor(Color.darkGray);
      g.fillRect(0, 0, getWidth(), getHeight());

      g.setColor(Color.YELLOW);
      g.drawRect(paddleX, paddleY, paddleWidth, paddleHeight);
      g.fillRect(paddleX, paddleY, paddleWidth, paddleHeight);
      System.out.println("-----");

    }

    class classKeyEvents  implements KeyListener
    {

        public void keyTyped(KeyEvent e) { }

        public void keyPressed(KeyEvent e)
        {
           if(e.getKeyCode() == KeyEvent.VK_KP_UP)
           {

               System.out.println("...");
               Pong.paddleY -= Pong.upSpeed;
               Pong.frame.repaint();
           }

           else if(e.getKeyCode() == KeyEvent.VK_KP_DOWN)
           {
               System.out.println("...");
               Pong.paddleY += Pong.downSpeed;
               Pong.frame.repaint();
           }
        }

        public void keyReleased(KeyEvent e)
        {

        }

    }

    class mainLoop implements Runnable
    {
        public void run() 
        {
            while(true)
            {

                try 
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException ex) 
                {
                    Logger.getLogger(Pong.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

    }

    public static void main(String[] args) 
    {
      Pong p = new Pong();              
      p.creatWindow();

    }

}

someone please help me to find solution for this problem it will help me a lot


Solution

  • I think I found what might cause your issues.

    KeyEvent.VK_KP_UP 
    

    are the KeyEvents on the KEYPAD, therefore the "KP".

    Use

    KeyEvent.VK_UP / KeyEvent.VK_DOWN
    

    instead.