How to focus key listeners

Hi, I am trying to make my game respond to the key arrows. I can't get the keylistener to acknowledge that the key has been pressed and I think I need to focus the listener. I can't figure out how to do that. Here is some of the code:

public TetrisGUI()

{

frame.setContentPane(this);

frame.setLocation(FRAME_UPPER_LEFT_X_COORD,FRAME_UPPER_LEFT_Y_COORD);

frame.setSize(new Dimension(FRAME_WIDTH,FRAME_HEIGHT));

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame.setTitle(WINDOW_TITLE);

frame.setVisible(true);

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

requestFocusInWindow(true);

RepaintManager r = RepaintManager.currentManager(this);

r.setDoubleBufferMaximumSize(new Dimension(FRAME_WIDTH,FRAME_HEIGHT));

repaint();

The code I have for the keylistener is:

publicvoid keyTyped(KeyEvent e){

int key=e.getKeyCode();

if(key==KeyEvent.VK_LEFT)

{

GameDisplay=FigureOne.moveLeft(GameDisplay);

repaint();

System.out.println("Move Left");

}

elseif(key==KeyEvent.VK_RIGHT)

{

GameDisplay=FigureOne.moveRight(GameDisplay);

repaint();

System.out.println("Move Right");

}

}//End keyTyped()

If you know what to do please let me know.

Thanks,

Mike

[2191 byte] By [Mag9102a] at [2007-10-2 12:42:35]
# 1

take a peek at the api docs for KeyEvent, basically keyTyped does not recognise

getKeyCode() - try using keyPressed

if this is to work on a JPanel, you will also need this

panel.setFocusable(true); (or just setFocusable(true))

also, this line

requestFocusInWindow(true);

will only work if called after the component is visible (and focusable etc)

Michael_Dunna at 2007-7-13 9:48:24 > top of Java-index,Security,Event Handling...
# 2
Thanks for the tips, but the keyPressed function you told me isn't defined. Is there something I need to import? Here is the code:int key=e.keyPressed();Mike
Mag9102a at 2007-7-13 9:48:24 > top of Java-index,Security,Event Handling...
# 3
changepublic void keyTyped(KeyEvent e)topublic void keyPressed(KeyEvent e)
Michael_Dunna at 2007-7-13 9:48:24 > top of Java-index,Security,Event Handling...
# 4
It works now, thanks for the helpMike
Mag9102a at 2007-7-13 9:48:24 > top of Java-index,Security,Event Handling...