11

I know how to implement a key listener; that's not the problem.

public void keyTyped(KeyEvent event) {
    if (event.getKeyChar() == KEY_LEFT) {
        cTDirection = LEFT;
    }
    if (event.getKeyChar() == 40) {
        cTDirection = DOWN;
    }
    if (event.getKeyChar() == 39) {
        cTDirection = RIGHT;
    }
    if (event.getKeyChar() == 38) {
        cTDirection = UP;
    }
}

What do I put where the LEFT_KEY / 40 / 39 / 38? When I created a keylistener and type in the keys, I believe I got 37 - 40. I don't know what to put there to listen for just the arrow keys.

1
  • this should have "AWT" in at list tag, or in the title.
    – user9999
    Dec 4, 2020 at 21:54

7 Answers 7

17

I would recommend using:

if (event.getKeyCode() == KeyEvent.VK_UP) {
...
}

repeating with VK_DOWN, VK_LEFT, VK_RIGHT.

There are seperate codes for the numeric keypad: VK_KP_UP, VK_KP_DOWN, VK_KP_LEFT, VK_KP_RIGHT if you need them.

See KeyEvent for all of the codes.

2
  • I want just the regular arrow keys... that does not affect my arrow keys!
    – Zeveso
    Dec 23, 2010 at 20:35
  • 3
    Those are the codes for 'regular arrow keys'. If you are still having problems, I suggest that you post more of your code because you have a problem somewhere else.
    – robert_x44
    Dec 23, 2010 at 21:13
3

KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, etc.

Also, you should use getKeyCode, not getKeyChar. getKeyChar is for keys that actually correspond to characters (letters, numbers, spaces, etc.).

5
  • I want just the regular arrow keys... that does not affect my arrow keys!
    – Zeveso
    Dec 23, 2010 at 20:34
  • @user - Yes it does affect the arrow keys.
    – Brad Mace
    Dec 23, 2010 at 20:36
  • @user516664 that is the code for the "regular" arrow keys. Maybe you want the numeric keypad arrows instead, in which case use VK_KP_LEFT, etc. If that still doesn't work then tell us what getKeyCode is returning for your "regular arrow keys". Dec 23, 2010 at 20:39
  • I actually did see an example that worked with VK_LEFT and all working, but it wasn't for me - it was returning 37 thought 40 in the order LEFT TOP RIGHT DOWN
    – Zeveso
    Dec 24, 2010 at 5:49
  • @user516664: Uh... VK_LEFT=37, VK_UP=38, VK_RIGHT=39, VK_DOWN=40. See the table here: download.oracle.com/javase/6/docs/api/… Dec 24, 2010 at 7:09
2

Use

if ( e.getKeyCode() == KeyEvent.VK_LEFT){
     //Do something
}

The other keys are:

KeyEvent.VK_UP

KeyEvent.VK_RIGHT

KeyEvent.VK_DOWN

1
  • I want just the regular arrow keys... that does not affect my arrow keys!
    – Zeveso
    Dec 23, 2010 at 20:33
2

Here is what I did to make it work:

public void keyPressed (KeyEvent e) {
        int c = e.getKeyCode ();
        if (c==KeyEvent.VK_UP) {                
            b.y--;   
        } else if(c==KeyEvent.VK_DOWN) {                
            b.y++;   
        } else if(c==KeyEvent.VK_LEFT) {                
            b.x--;   
        } else if(c==KeyEvent.VK_RIGHT) {                
            b.x++;   
        }
        System.out.println (b.x);
        b.repaint ();
    }

For me it isn't working if I put it in KeyPressed but works fine if I put it in KeyTyped.

1
  • Please don't use some random mark down which you have seen elsewhere. Use the buttons presented in the edit box's toolbar and read up, how to format your code and text, starting with the FAQ. Thank you, and welcome. Apr 29, 2012 at 13:48
1

Use the getKeyCode() method and compare the returned value agains KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP and KeyEvent.VK_DOWN constants.

1
  • I want just the regular arrow keys... that does not affect my arrow keys!
    – Zeveso
    Dec 23, 2010 at 20:34
0

first declare init method

public void init(){

this.addKeyListener(new keyb());}

then use inner class which implements KeyListner

class keyb implements KeyListener{

    public void keyPressed (KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_UP){
        y-=50;

    }else if(e.getKeyCode()==KeyEvent.VK_DOWN){
        y+=50;
    }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){            
        x+=50;
    }else if(e.getKeyCode()==KeyEvent.VK_LEFT){
        x-=50;
    }

    repaint();

    }
    public void keyReleased (KeyEvent e){}
    public void keyTyped (KeyEvent e){}
    }

you can also use adapter instead of writing Keyreleased & keyTyped .... as you know

-1

btw in KeyAdapter -> keyTyped getCharCode() didn't work You should try keyPressed or keyReleased

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.