title: How do I get KeyListener working?
body: <p>I do apologize, I know this has been asked before. The other posts suggest just using keybindings, but I'd much prefer to use this method as I would like the movement of the robot to stop with keyrelease. </p>
  
  <pre><code>import java.io.BufferedReader;
  import javax.swing.AbstractButton;
  import javax.swing.JButton;
  import javax.swing.JPanel;
  import javax.swing.JFrame;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintStream;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.awt.Button;
  import java.awt.Component;
  import java.awt.Container;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import javax.swing.BoxLayout;
  
  public class Robot implements KeyListener {
      public static JButton a, w, s, d, space, ctrl;
      public static char input;
      public static String takeoff = "{\"op\":\"publish\",\"topic\":\"/ardrone/takeoff\",\"msg\":{}}";
      public static String landing = "{\"op\":\"publish\",\"topic\":\"/ardrone/land\",\"msg\":{}}";
      public static String stop = "{\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":0,\"y\":0,\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":0}}}";
  
  
      private static void createAndShowGUI() {
          //Create and set up the window.
          JFrame frame = new JFrame("Robot Controller");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          addComponentsToPane(frame.getContentPane());
          frame.pack();
          frame.setVisible(true);
          frame.setSize(800, 600);
          frame.add(a);
          frame.add(w);
          frame.add(d);
          frame.add(s);
          frame.add(space);
          frame.add(ctrl);
  
  
          Socket mysocket = null;
          PrintStream output = null;
          try {
              // opens socket
              mysocket = new Socket("lear.cs.okstate.edu", 9095);     
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          try {
              // opens stream to write to socket
              output = new PrintStream(mysocket.getOutputStream());
              output.print(takeoff);
              } catch (IOException e){
              System.out.print(e);
              }
          }
      public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_A) {
              System.exit(0);
          }
          if (e.getKeyCode() == KeyEvent.VK_S) {
  
          }
          if (e.getKeyCode() == KeyEvent.VK_D) {
  
          }
          if (e.getKeyCode() == KeyEvent.VK_W) {
  
          }
          if (e.getKeyCode() == KeyEvent.VK_S) {
  
          }
          if (e.getKeyCode() == KeyEvent.VK_SPACE) {
  
          }
          if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
  
          }
      }
  
      public void keyReleased(KeyEvent e) {
          // TODO Auto-generated method stub
      }
  
      public void keyTyped(KeyEvent e) {
          // TODO Auto-generated method stub
      }
  
      public static void addComponentsToPane(Container pane) {
          pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
          a = new JButton("Move left: A");       
          w = new JButton("Move up: W");
          d = new JButton("Move right: D");
          s = new JButton("Move down: S");
          space = new JButton("Takeoff: spacebar");
          ctrl = new JButton("Land: control");
      }
  
      public static void main(String[] args) {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  }
  </code></pre>