title: Using Key Bindings with Arrow Keys
body: <p>I'm creating a game that uses the arrow keys to move a sprite. I've added key bindings for the arrow keys and the letter n, but arrow keys aren't working. Here's my code:</p>
  
  <pre><code>public class MyPanel extends JPanel {
  
      Sprite sprite = new Sprite();
  
      Timer gameClock = new Timer(DELAY, new ActionListener(){
          public void actionPerformed(ActionEvent e){
              sprite.move();
              // omit other methods
          }
      });
  
      // omit other member variables
  
      public MyPanel(){
          Abstract Action newGameAction = new AbstractAction("new game") {
  
              public void actionPerformed(ActionEvent e){
                  doNewGame();
              }
          }
  
          setFocusable(true);
  
          addKeyBinding(new Pair&lt;String, Action&gt;("N", newGameAction));
          ArrayList&lt;Pair&lt;String, Action&gt;&gt; pairs = sprite.findKeyBindingPairs();
          for (Pair&lt;String, Action&gt; p : pairs)
              addKeyBindings(p);
  
  
          gameClock.start();
  
          // omit other panel init
  
      }
  
      private void addKeyBindings(Pair&lt;String, Action&gt; pair) {
          String key = pair.getFirstElement();
          Action action = pair.getSecondElement();
          String desc = action.getValue(AbstractAction.NAME).toString();
          getInputMap().put(KeyStroke.getKeyStroke(key), desc);
          getActionMap().put(desc, action);
      }
  
      // omit other methods
  }
  
  
  public class Sprite {
  
      private class ChangeDirAction extends AbstractAction {
          int dx, dy;
  
          ChangeDirAction(String name, int dx, int dy){
              super(name);
              this.dx = dx;
              this.dy = dy;
          }
  
          public void actionPerformed(ActionEvent e){
              setVelocity(dx, dy);
          }
      }
  
      private int dx_, dy_;
      Point pos;
  
      // omit other instance variables
  
      public void move(){
          // With printlns dx_ and dy_ are both 0 here. Why?
          Point newPos = new Point(pos);
          newPos.translate(dx_, dy_);
  
          // omit code to test whether newPos is valid
  
          if (isWall(newPos) || isOutsidePanel(newPos))
              setVelocity(0, 0);
          else
              pos = newPos;
      }
  
  
      private void setVelocity(int dx, int dy){
          dx_ = dx;
          dy_ = dy;
          // With printlns dx_ and dy_ change when arrow keys are pressed
      }
  
      public ArrayList&lt;Pair&lt;String, Action&gt;&gt; findKeyBindingPairs(){
          Pair&lt;String, Action&gt; leftPair = new Pair&lt;String, Action&gt;("LEFT", new ChangeDirAction("left", -1, 0));
          Pair&lt;String, Action&gt; rightPair = new Pair&lt;String, Action&gt;("RIGHT", new ChangeDirAction("right", 1, 0));
          Pair&lt;String, Action&gt; upPair = new Pair&lt;String, Action&gt;("UP", new ChangeDirAction("up", 0, -1));
          Pair&lt;String, Action&gt; downPair = new Pair&lt;String, Action&gt;("DOWN", new ChangeDirAction("down", 0, 1));
          ArrayList&lt;Pair&lt;String, Action&gt;&gt; result = new ArrayList&lt;Pair&lt;String, Action&gt;&gt;();
          result.add(leftPair);
          result.add(rightPair);
          result.add(upPair);
          result.add(downPair);
          return result;
      }
  
      // omit other methods
  
  }
  </code></pre>