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.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
Zeveso
  • 1,274
  • 3
  • 21
  • 41

7 Answers7

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.

robert_x44
  • 9,224
  • 1
  • 32
  • 37
  • I want just the regular arrow keys... that does not affect my arrow keys! – Zeveso Dec 23 '10 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 '10 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.).

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • I want just the regular arrow keys... that does not affect my arrow keys! – Zeveso Dec 23 '10 at 20:34
  • @user - Yes it **does** affect the arrow keys. – Brad Mace Dec 23 '10 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". – Laurence Gonsalves Dec 23 '10 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 '10 at 05:49
  • @user516664: Uh... VK_LEFT=37, VK_UP=38, VK_RIGHT=39, VK_DOWN=40. See the table here: http://download.oracle.com/javase/6/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_LEFT – Laurence Gonsalves Dec 24 '10 at 07:09
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.

user unknown
  • 35,537
  • 11
  • 75
  • 121
Abhas Tandon
  • 1,859
  • 16
  • 26
  • 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. – user unknown Apr 29 '12 at 13:48
2

Use

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

The other keys are:

KeyEvent.VK_UP

KeyEvent.VK_RIGHT

KeyEvent.VK_DOWN

Andreas Løve Selvik
  • 1,262
  • 16
  • 25
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.

Progman
  • 16,827
  • 6
  • 33
  • 48
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

ahmed reda
  • 153
  • 2
  • 5
-1

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

Elendil
  • 11