para eso hay que implementar la interfaz KeyListener
aqui te pongo un codigo de ejemplo, es un applet pero facilmente lo puedes adaptar a tus necesidades
Código PHP:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
public class KeyEventDemo extends JApplet implements KeyListener, ActionListener {
JTextField displayArea;
JTextField typingArea;
public void init() {
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
displayArea = new JTextField(20);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(typingArea, BorderLayout.NORTH);
contentPane.add(displayArea, BorderLayout.CENTER);
setContentPane(contentPane);
}
public void keyPressed(KeyEvent e) {
displayInfo(e);
}
protected void displayInfo(KeyEvent e){
int keyCode = e.getKeyCode();
String keyText = e.getKeyText(keyCode);
displayArea.setText(keyText);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {}
}
Saludos