title: Why can't i use keyPressed anymore once the if-statement is true in the run()-methode?
body: <p>I'm trying to make a kind of brickbreaker game but once I press the start button ("starten" = true). I can't use the keyPressed()-utility anymore but before i pressed the start button I could use the keyPressed()-utility. Could somebody please tell me why I suddenly can't use the keyPressed()-utility anymore and give me a possible solution too?</p>
  
  <pre><code>import java.util.*;
  import java.awt.*;
  import java.awt.event.*;
  
  import javax.swing.*;
  
  public class VGKernel extends JPanel implements KeyListener {
  
  public Rectangle screen, ball, block; // The screen area and ball location/size.
  public Rectangle bounds;  // The boundaries of the drawing area.
  public JFrame frame; // A JFrame to put the graphics into.
  public VGTimerTask vgTask; // The TimerTask that runs the game.
  public boolean down, right, starten = false; // Direction of ball's travel.
  public JButton start;
  
  public VGKernel(){
      super();
      screen = new Rectangle(0, 0, 600, 400);
      ball   = new Rectangle(0, 0, 20, 20);
      block = new Rectangle(260, 350, 40, 10);
      bounds = new Rectangle(0, 0, 600, 400); // Give some starter values.
      frame = new JFrame("VGKernel");
      vgTask = new VGTimerTask();
      addKeyListener(this);
      setFocusable(true);
      start = new JButton("Start");
      start.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              starten = true;
          }
      });
      add(start);
  }
  
  class VGTimerTask extends TimerTask{
      public void run(){
          repaint();
  
          if(starten){
          moveBall();
          frame.repaint();
          }
      }
    }
  
  // Now the instance methods:
    public void paintComponent(Graphics g){
      // Get the drawing area bounds for game logic.
      bounds = g.getClipBounds();
      // Clear the drawing area, then draw the ball.
      g.clearRect(screen.x, screen.y, screen.width, screen.height);
      g.fillRect(ball.x, ball.y, ball.width, ball.height);
      g.fillRect(block.x, block.y, block.width, block.height);
    }
  
    public void moveBall(){
    // Ball should really be its own class with this as a method.
      if (right) ball.x+=1; // If right is true, move ball right,
      else ball.x-=1;       // otherwise move left.
      if (down)  ball.y+=1; // Same for up/down.
      else ball.y-=1;
      if (ball.x &gt; (bounds.width - ball.width)) // Detect edges and bounce.
        { right = false; ball.x = bounds.width -  ball.width; }
      if (ball.y &gt; (bounds.height - ball.height))
        { down  = false; ball.y = bounds.height - ball.height;}
      if (ball.x == 0) { right = true; ball.x = 0; }
      if (ball.y == 0) { down  = true; ball.y = 0; }
    }
  
    public void keyPressed(KeyEvent evt) {
        if(evt.getKeyCode() == KeyEvent.VK_G &amp;&amp; block.x &gt; 0) {
            block.x -= 20;
        }
  
        if(evt.getKeyCode() == KeyEvent.VK_H &amp;&amp; block.x &lt; 540) {
            block.x += 20;
        }
    }
  
    public void keyTyped(KeyEvent evt){  }
    public void keyReleased(KeyEvent evt){ }
  
    public void startActionPerformed(java.awt.event.ActionEvent evt) {
        starten = true;
    }
  
    public static void main(String arg[]){
      java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer.
      VGKernel panel = new VGKernel(); // Create and instance of our kernel.
  
      // Set the intial ball movement direction.
      panel.down = true;
      panel.right = true;
  
      // Set up our JFRame
      panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      panel.frame.setSize(panel.screen.width, panel.screen.height);
      panel.frame.setContentPane(panel); 
      panel.frame.setVisible(true);
  
      // Set up a timer to do the vgTask regularly.
      vgTimer.schedule(panel.vgTask, 0, 10);
    }
  </code></pre>
  
  <p>}</p>