-2

In a java program I've added keylistener and I want to check if anyone has pressed the arrow keys or not.Can anyone please help me since I dont know any ascii codes for arrow keys? If possible please give a sample program also.

4

1 回答 1

0

KeyEvent.VK_UP KeyEvent.VK_DOWN KeyEvent.VK_LEFT KeyEvent.VK_RIGHT

These are the conditions that you test e.getKeyCode() for.

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    switch( key ) { 
        case KeyEvent.VK_UP:
            // up 
            break;
        case KeyEvent.VK_DOWN:
            // down 
            break;
        case KeyEvent.VK_LEFT:
            // left
            break;
        case KeyEvent.VK_RIGHT :
            // right
            break;
     }
} 

EDIT: I just see now a duplicate of this question here How to check if the key pressed was an arrow key in Java KeyListener?

于 2013-03-13T06:08:41.013 回答