I am trying to make a simple game that requires movement. The only issue is when I type a key, every key returns a keyCode of '0'. I have read around Stack Overflow a little bit, and all the answers I found to my question were to use KeyPressed instead of KeyTyped. The only problem is that I am using KeyPressed, and all the keys are still returning 0.
My KeyListener code below:
Sidenote: print(bool) = System.out.print(bool), I was sick of writing the whole thing.
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W) {up = true; print(up);}
if(e.getKeyCode() == KeyEvent.VK_A) {left = true; print(left);}
if(e.getKeyCode() == KeyEvent.VK_S) {down = true; print(down);}
if(e.getKeyCode() == KeyEvent.VK_D) {right = true; print(right);}
System.out.println("Pressed " + e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W) {up = false;}
if(e.getKeyCode() == KeyEvent.VK_A) {left = false;}
if(e.getKeyCode() == KeyEvent.VK_S) {down = false;}
if(e.getKeyCode() == KeyEvent.VK_D) {right = false;}
//System.out.println("Released " + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {}
});
This will most likely be an easy solution, but I am not very experienced with this area of coding. Thanks for any help.