import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.Color; class Program01 extends JFrame { JPanel panel; Program01() { addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { OnKeyPressed(e); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} }); panel = new JPanel(); panel.setBounds(10, 300, 100, 30); panel.setBackground(Color.green); add(panel); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 400); setVisible(true); } private void OnKeyPressed(KeyEvent e) { System.out.println(e.getExtendedKeyCode()); if(e.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("Enter"); } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { System.out.println("Jobbra"); } if(e.getKeyCode() == KeyEvent.VK_LEFT) { System.out.println("Balra"); } System.out.println(e.getKeyCode()); } public static void main(String args[]) { new Program01(); } }