0

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.

  • If your code is identical to the example, then no, you are not using `keyPressed()`. Copy your code from `keyTyped()` in there and see what happens. – Dragondraikk Aug 05 '15 at 15:19

1 Answers1

0

I am using KeyPressed, and all the keys are still returning 0.

This is your implementation of the keyPressed handler:

@Override
public void keyPressed(KeyEvent e) {}

It seems self-explanatory.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436