title: Why does my KeyPressed event randomly stop working?
body: <p>I am new to Java, and am trying to learn GUI basics by making a simple application.<br />My application is just a simple button that, when clicked, changes its name to the number of times it has been clicked.</p>
  <p>I want to be able to reset the counter/button name when the user presses the &quot;r&quot; key, but for some reason it only works before the button has been clicked.<br />After the counter goes over zero, the key press is no longer detected.</p>
  <p>I have tried everything I can find on the internet, including:</p>
  <ul>
  <li>Multiple different Key Listeners</li>
  <li><code>frame.setFocusable(true);</code></li>
  <li>Youtube tutorials</li>
  <li>Existing Stack Overflow posts (none seem to match this problem)</li>
  <li>Debugging (not showing any errors)</li>
  </ul>
  <p>I can't figure out what's going on. If someone could help, it would be MUCH appreciated. My code is below. (I don't know which part of it is causing the problem, so I can't make it too short or I might crop out something important. It's only 90 lines.)</p>
  <pre><code>import java.awt.AWTException;
  import java.awt.BorderLayout;
  import javax.swing.*;
  import java.awt.event.*;
  
  public class Clicker implements ActionListener {
      JMenu m1;
      JMenuItem m11;
      JMenuItem m22;
      JButton button;
      int counter = 0;
      
      public Clicker() throws AWTException {
          JFrame frame = new JFrame(&quot;Click Counter&quot;);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(300,300);
          
          JMenuBar mb = new JMenuBar();
          JMenu m1 = new JMenu(&quot;File&quot;);
          mb.add(m1);
          m11 = new JMenuItem(&quot;Reset&quot;);
          m22= new JMenuItem(&quot;Quit&quot;);
          m1.add(m11);
          m1.add(m22);
          button = new JButton(&quot;Click&quot;);
  
          frame.getContentPane().add(BorderLayout.CENTER, button);
          frame.getContentPane().add(BorderLayout.NORTH, mb);
          frame.setVisible(true);
          frame.setFocusable(true);
          
          m22.setToolTipText(&quot;⌘Q&quot;);
          m11.setToolTipText(&quot;R&quot;);
          
          button.addActionListener(this);
          m11.addActionListener(this);
          m22.addActionListener(this);
          
          frame.addKeyListener(new KeyListener() {
  
              @Override
              public void keyTyped(KeyEvent e) {
                  //nothing
                  
              }
  
              @Override
              public void keyPressed(KeyEvent e) {
                  int keycode = e.getKeyCode();
                  switch(keycode) {
                      case KeyEvent.VK_R:
                          System.out.println(&quot;R Pressed&quot;);
                          counter = 0;    
                          button.setText(&quot;Clicks: &quot; + counter);
                          break;
                  }
              }
  
              @Override
              public void keyReleased(KeyEvent e) {
                  //nothing
                  
              }
              
          });
          
      }
  
      public static void main(String[] args) throws AWTException {
          new Clicker();
      }
  
      public void actionPerformed(ActionEvent evt) {
          if (evt.getSource()==button) {
              counter++;  
              button.setText(&quot;Clicks: &quot; + counter);
          }
          else if (evt.getSource()==m11) {
              counter = 0;    
              button.setText(&quot;Clicks: &quot; + counter);
          }
          else if (evt.getSource()==m22) {
              System.exit(0);
          }
          
      }
  
  }
  </code></pre>