/** The implemented key-pressed event, for sending commands to the robot. */ @Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); System.out.println(code); // Send a command depending on the key code if (code != KeyCode.SPACE_BAR && code != KeyCode.G && code != KeyCode.O && lastCode == code) { power += 10; } else { power = 10; } lastCode = code; switch (code) { // Left Arrow: Turn Left case KeyCode.LEFT_ARROW: tcpClient.sendCommand((byte) 3, power); break; // Up Arrow: Move Forward case KeyCode.UP_ARROW: tcpClient.sendCommand((byte) 1, power); break; // Right Arrow: Turn Right case KeyCode.RIGHT_ARROW: tcpClient.sendCommand((byte) 4, power); break; // Down Arrow: Move Backward case KeyCode.DOWN_ARROW: tcpClient.sendCommand((byte) 2, power); break; // s: Stop case KeyCode.S: tcpClient.sendCommand((byte) 5, power); break; // Space bar: Kick case KeyCode.SPACE_BAR: tcpClient.sendCommand((byte) 6, 5000); break; // g: Grab case KeyCode.G: tcpClient.sendCommand((byte) 7, 0); break; case KeyCode.O: tcpClient.sendCommand((byte) 9, 0); break; } }
/** * Begins listening to key commands and sending robot data. * * @throws IOException */ public void run() throws IOException { try { tcpClient = new TCPClient(robotID); tcpClient.connect(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Couldn't connect to the relay."); return; } JFrame frame = new JFrame("control"); frame.setBounds(500, 500, 200, 50); frame.setAlwaysOnTop(true); JLabel label = new JLabel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label.setText("Select and Pess a Key"); frame.add(label); frame.addKeyListener(this); frame.setVisible(true); }