rokoblox
rokoblox

Reputation: 79

KeyListener not working with JFrame Canvas

I'm still new to java and I can't get my JFrame to detect key presses/releases, Here are the classes used

Main:

package initilizer;
import java.awt.Canvas;

import java.awt.Graphics;
import javax.swing.JFrame;
import input.Keyboard;

public class Main extends Canvas{

    static boolean rRunning = true;

    static int width = 640;
    static int height = 480;
    int cx = width/2;
    int cy = height/2;
    boolean initilized = false;

    Camera cam = new Camera(1.0, 5.0, 3.0);
    Camera cam1 = new Camera(10.0, 50.0, 30.0);
    long lastFpsCheck = System.currentTimeMillis();

    public static JFrame frame = new JFrame("3D Engine");

    Keyboard keyboard = new Keyboard();


    public static void main(String[] args) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Canvas canvas = new Main();
        canvas.setSize(width, height);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    void init() {
        // Nothing currently
    }

    double[] rotate2D(double[] pos,double[] rot) {
        double x = pos[0];
        double y = pos[1];

        double s = rot[0];
        double c = rot[1];

        double[] result = {(x * c) - (y * s), (y * c) + (x * s)};

        return result;
    }


    public void paint(Graphics g) {
        if (initilized == false) {
            initilized = true;
            init();
        }

        // Storing start time for FPS Counting
        long startTime = System.currentTimeMillis();


        g.dispose();

        if (keyboard.getA() == true) {
            System.out.println("A HELD!!");
        }

        // Limiting FPS
        try {
            Thread.sleep(16);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Calculating FPS
        long endTime = System.currentTimeMillis();
        if ((lastFpsCheck + 1000) < endTime) {
            lastFpsCheck = endTime;
            double delta_time = (endTime - startTime);
            frame.setTitle("3D Engine - FPS: " + (int) (1000/delta_time));
        }

        // Draw next frame
        repaint();
    }

}

Keyboard:

package input;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import initilizer.Main;

public class Keyboard implements ActionListener, KeyListener{

    public boolean W_HELD = false;
    public boolean A_HELD = false;
    public boolean S_HELD = false;
    public boolean D_HELD = false;

    public boolean E_HELD = false;
    public boolean Q_HELD = false;

    public Keyboard() {
        Main.frame.addKeyListener(this);
    }

    public boolean getW() {
        return W_HELD;
    }

    public boolean getA() {
        //System.out.println(A_HELD);
        return A_HELD;
    }

    public boolean getS() {
        return S_HELD;
    }

    public boolean getD() {
        return S_HELD;
    }

    public boolean getE() {
        return E_HELD;
    }

    public boolean getQ() {
        return Q_HELD;
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("HII");
          int key = e.getKeyCode();

          if (key == KeyEvent.VK_W) {
              W_HELD = true;
          }

          if (key == KeyEvent.VK_A) {
              A_HELD = true;
          }

          if (key == KeyEvent.VK_S) {
              S_HELD = true;
          }

          if (key == KeyEvent.VK_D) {
              D_HELD = true;
          }

          if (key == KeyEvent.VK_E) {
              E_HELD = true;
          }

          if (key == KeyEvent.VK_Q) {
              Q_HELD = true;
          }
    }

    public void keyReleased(KeyEvent e) {
          int key = e.getKeyCode();

          if (key == KeyEvent.VK_W) {
              W_HELD = false;
          }

          if (key == KeyEvent.VK_A) {
              A_HELD = false;
          }

          if (key == KeyEvent.VK_S) {
              S_HELD = false;
          }

          if (key == KeyEvent.VK_D) {
              D_HELD = false;
          }

          if (key == KeyEvent.VK_E) {
              E_HELD = false;
          }

          if (key == KeyEvent.VK_Q) {
              Q_HELD = false;
          }
    }

    public void keyTyped(KeyEvent e) {
        // Unmodified
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

}

I've been stuck trying to fix this but couldn't, Tried adding key listeners through the main class but that also didn't work, If you know the problem then please tell me, Thank you.

Upvotes: 0

Views: 544

Answers (1)

FailingCoder
FailingCoder

Reputation: 767

You have to add a KeyListener to your Canvas to be able to receive input from it, e.g.

canvas.add(new Keyboard());

Upvotes: 2

Related Questions