title: How can I connect the WASD-keys to specific events?
body: <p>I am pretty new to programming, but I am curios and want to learn more. Currently, I’m trying to make a very simple program where I can move a 2-dimenstional ball around on the screen. However, currently I have to use buttons in the program to move the ball. I would like to be able to use the WASD or such. I have searched through this site, but haven’t found something that’s basic enough.
  My question is actually quite simple – how can I make it so when I press a key, for example W, the ball moves in that direction (or whatever event I add to it)? I saw something about like this on this site: 
          ( g.getKeyCode() == KeyEvent.VK_LEFT)</p>
  
  <p>However, the above described code doesn’t work to make the ball move. Is there any simple code to connect a pressed key and an event? Notice that I use NetBeans.</p>
  
  <p>Below, you can see a description of what I’m trying to work with (and which doesn’t work):</p>
  
  <p>EDIT:
  To clerify, here is the whole code I'm working with, as an example:</p>
  
  <pre><code>    package gamePanel;
      import java.awt.Color;
      import java.awt.Graphics;
  
      public class GamePanel extends javax.swing.JPanel {
  
  
  public GamePanel() {
      initComponents();
  }
  
  @SuppressWarnings("unchecked")
  // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
  private void initComponents() {
  
      btnUp = new javax.swing.JButton();
      btnDown = new javax.swing.JButton();
      btnLeft = new javax.swing.JButton();
      btnRight = new javax.swing.JButton();
  
      btnUp.setText("Up");
      btnUp.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              btnUpActionPerformed(evt);
          }
      });
  
      btnDown.setText("Down");
      btnDown.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              btnDownActionPerformed(evt);
          }
      });
  
      btnLeft.setBackground(new java.awt.Color(140, 140, 140));
      btnLeft.setText("Left");
      btnLeft.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              btnLeftActionPerformed(evt);
          }
      });
  
      btnRight.setBackground(new java.awt.Color(140, 140, 140));
      btnRight.setText("Right");
      btnRight.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              btnRightActionPerformed(evt);
          }
      });
  
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
              .addGap(84, 84, 84)
              .addComponent(btnLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                  .addComponent(btnUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(btnDown, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addComponent(btnRight)
              .addContainerGap(129, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
              .addContainerGap(238, Short.MAX_VALUE)
              .addComponent(btnUp)
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(btnDown)
                  .addComponent(btnLeft)
                  .addComponent(btnRight))
              .addGap(10, 10, 10))
      );
  }// &lt;/editor-fold&gt;                        
  
  int x = 0;
  int y = 0;
  
  private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {                                      
  
      y -= 10;
      repaint();
  }                                     
  
  private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {                                        
  
      y += 10;
      repaint();
  }                                       
  
  private void btnLeftActionPerformed(java.awt.event.ActionEvent evt) {                                        
  
      x -= 10;
      repaint();
  }                                       
  
  private void btnRightActionPerformed(java.awt.event.ActionEvent evt) {                                         
  
      x += 10;
      repaint();
  }                                        
  
  protected void paintComponent(Graphics g){
      super.paintComponent(g);
  
      g.setColor(Color.BLACK);
      g.fillOval(x, y, 25, 25);
  }
  
  
  // Variables declaration - do not modify                     
  private javax.swing.JButton btnDown;
  private javax.swing.JButton btnLeft;
  private javax.swing.JButton btnRight;
  private javax.swing.JButton btnUp;
  // End of variables declaration                   
  </code></pre>
  
  <p>}</p>