title: Keep focus on Java game for Key Listener
body: <p>I'm working on Java game development, and an issue I'm running into is implementing key strokes. The code I have properly implements the KeyListeners, but as soon as I click on the game window or outside of it, the KeyListener stops working. </p>
  
  <p>I'm sure this is a focus issue as I can minimize the window and bring it back up and the KeyListener will work again.</p>
  
  <p>My setup is a JFrame that contains a Canvas object for drawing. I have a regular KeyListener that I add to the JFrame.</p>
  
  <p>So, I'm guessing, when I click on the game, the Canvas gains focus? It shouldn't though, as I set isFocasable() to false.</p>
  
  <p>Here is some of the code I'm using, just so there isn't any confusion:</p>
  
  <p>Here, I create the screen and canvas</p>
  
  <pre><code>public final void initScreen(DisplayMode dm, boolean fs, boolean vs)
  {
      screen = new EScreen (dm, fs, vs); 
      canvas = new EGameCanvas (screen);
      canvas.setFocusable (false);
      screen.add(canvas);
  
      keyMapManager = KeyMapManager.getInstance (this);
  
      if (fs) {
          screen.setFullScreen ();
      }
      else {
          screen.setWindowed ();
      }
  }
  </code></pre>
  
  <p>This is where I add the KeyListener (Note that a KeyMap implements KeyListener and nothing more)</p>
  
  <pre><code>public boolean setActiveKeyMap(String newKeyMap)
  {
      if (keyMaps.containsKey(newKeyMap)) {
          screen.removeKeyListener (activeKeyMap);
          activeKeyMap = keyMaps.get (newKeyMap);
          screen.addKeyListener (keyMaps.get (newKeyMap));
  
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>And here is my keyPressed event that simply changed a variable</p>
  
  <pre><code>public void keyPressed(KeyEvent ev) {
      if (ev.getKeyCode() == KeyEvent.VK_ESCAPE)
          game.setIsRunning (false);
  }
  </code></pre>
  
  <p>Please note, all this code works just fine. That is, until I click on the game window. I don't have any MouseListeners implemented anywhere. Also, I do implement this code:</p>
  
  <pre><code>screen.setFocusTraversalKeysEnabled (false);
  </code></pre>
  
  <p>And finally, as you can see, my game handles both full screen mode and windowed mode. This problem occurs in both.</p>