[BUG] hasFocus() and isKeyDown() issues

Started by JoeK5142, August 04, 2012, 08:02:50

Previous topic - Next topic

JoeK5142

Hello all,

I'm using Slick2d and LWJGL to write a game.  I have two issues right now, and I was told that they are both related to LWJGL on the Slick2D forum, so I've come here for assistance. The first is that isKeyDown() will return true if the game window loses focus while they key in question is down, even after returning to the game and no longer holding the key down. In my particular case, this makes it so that if the game is alt+tabbed out of and brought back up, pressing enter will switch between fullscreen and windowed mode (when it should be alt+enter). They alt key returns to normal functionality afterwards.

The second issue stems from trying to fix the first. I was attempting to check the container with hasFocus(), then destroy() and create() the Keyboard once it came back into focus. This only seemed to work in windowed mode. When investigating, I found that hasFocus() always returns true in fullscreen mode, whether alt+tabbed or not.

Here are the relevant areas of the update() function:

Code:
 
public void update(GameContainer container, int delta) throws SlickException 
   {   
      if(!container.hasFocus())
      {
         System.out.println("Container does not have focus"); //Does not print when alt+tabbed in fullscreen, does in windowed
      }
      Input input = container.getInput();   
      input.enableKeyRepeat();
      
         
      if(input.isKeyPressed(Input.KEY_ENTER))
      {
         if(input.isKeyDown(Input.KEY_LALT) || input.isKeyDown(Input.KEY_RALT))
         {
            Vars.fullscreen = !Vars.fullscreen;
            container.setFullscreen(Vars.fullscreen);
         }
      }
...



If anyone can help me with these issues, it'd be greatly appreciated. Either getting hasFocus() to work properly with fullscreen enabled or forcing a key release on LALT and RALT could help, but I haven't been able to figure out how to do these things.

Thanks for reading.

kappa

The first issue already has a bug report found here including a possible fix/patch on that thread.