KeyListener + passing the focus properly:
@Override
public void keyTyped(KeyEvent ke) {
if(ke.getKeyChar() == KeyEvent.VK_LEFT){
//do something
}else if(ke.getKeyChar() == KeyEvent.VK_RIGHT){
//do something else
}
}
@Override
public void keyPressed(KeyEvent e) {
keyTyped(e);
}
also by input mapping:
private void inputMap(){
this.getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK\_LEFT, 0), "left");
this.getActionMap().put("left", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
//do something
}
});
}