title: How to detect keyboard input?
body: <p>I am trying to make a square in a <code>JFrame</code> to move around the frame by detecting keyboard input. I tried looking on the internet and that is where I got the <code>keyPressed()</code> method from. What I am looking for is how to declare it in the <code>main()</code> method so it will actually update x and y. Please try to keep it as simple as possible.</p>
  
  <pre><code>public class Game1{
  public static int x = 1;
  public static int y = 1;
  
  static GraphicsClass graphics = new GraphicsClass();
  static JFrame frame = new JFrame("Test");
  
  public static void main(String[] args){
      frameInit();
      while(true){
          //The problematic line, how do I declare it:
          keyPressed();
          frame.add(BorderLayout.CENTER, graphics);
          frame.repaint();
      }
  }
  
  public static void keyPressed(KeyEvent i){
      int e = i.getKeyCode();
      while(e == KeyEvent.VK_UP){
          y--;
      }
      while(e == KeyEvent.VK_DOWN){
          y++;
      }
      while(e == KeyEvent.VK_RIGHT){
          y++;
      }
      while(e == KeyEvent.VK_LEFT){
          x--;
      }
  }
  
  public static void frameInit(){
      frame.setSize(500, 500);
      frame.setVisible(true);
      frame.setResizable(false);
  }
  }
  </code></pre>