Here’s my keylistening code:
package cg.gui.event;
import cg.gui.SubcomponentIterator;
import java.awt.*;
import java.awt.event.*;
/**This class handles keyboard input for games.
* @author Steven Fletcher
* @since 2005/02/08
* @version 2007/02/28*/
public class GameKeyListener implements KeyListener {
/**Constructor
* @param shouldDisableMnemonics whether to disable keyboard mnemonics*/
public GameKeyListener(final boolean shouldDisableMnemonics) {
//set up the keyboard
this.shouldDisableMnemonics = shouldDisableMnemonics;
for(int iKeyState = 0; iKeyState < numKeys; iKeyState++)
aKeyState[iKeyState] = InputSourceState.UP;
} //end constructor
/**Forget all the current keyboard input.*/
public void clearInput() {
keyTyped = KeyEvent.CHAR_UNDEFINED;
for(int i = 0; i < numKeys; i++)
wasKeyPressed(i);
} //end clearInput
/**Get the Ascii value of the last key "typed" (according to KeyListener).
* @return the last character typed*/
public char getLastKeyTyped() {
char lastKeyTyped = keyTyped;
keyTyped = KeyEvent.CHAR_UNDEFINED;
return lastKeyTyped;
} //end getLastKeyTyped
/**Listen to a given Component.
* @param component the Component to listen to*/
public void listenTo(final Component component) {component.addKeyListener(this);}
/**Adds the specified KeyListener to all Components included in the specified Container.
* Note that the KeyListener won't be added to Components added to the Container later
* @param container the Container to listen to*/
public void listenToContainerAndSubcomponents(final Container container) {
//verify that the Container isn't null
if(container == null)
return;
container.addKeyListener(this);
//for each subcomponent of this ViewPane, add the global KeyListener
for(SubcomponentIterator iSubcomponent = new SubcomponentIterator(container);
iSubcomponent.hasNext(); )
{
Component subcomponent = iSubcomponent.next();
subcomponent.addKeyListener(this);
if(subcomponent instanceof Container)
listenToContainerAndSubcomponents((Container)subcomponent);
} //end for each subcomponent of the Component
} //end listenToContainerAndSubcomponents
/**Determines whether ANY key was pressed since the last time we checked. This
* method doesn't clear the states of any of the keys.
* @return whether ANY key was pressed since the last time we checked*/
public boolean wasAnyKeyPressed() {
return GameEventUtil.wasAnyInputSourcePressed(aKeyState);
} //end wasAnyKeyPressed
/**Determines whether ANY key was released since the last time we checked. This
* method doesn't clear the states of any of the keys.
* @return whether ANY key was released since the last time we checked*/
public boolean wasAnyKeyReleased() {
return GameEventUtil.wasAnyInputSourceReleased(aKeyState);
} //end wasAnyKeyReleased
/**Determines whether the key, keyCode, was pressed since the last time we checked.
* If it was pressed, it's state is cleared to UP.
* @param keyCode code of the key to check (see class KeyEvent in the Java API)
* @return whether the key, keyCode, was pressed since the last time we checked*/
public boolean wasKeyPressed(final int keyCode) {
return GameEventUtil.wasInputSourcePressed(aKeyState, keyCode);
} //end wasKeyPressed
/**Determines whether the key, keyCode, was released since the last time we checked.
* If it was released, it's state is cleared to UP
* @param keyCode code of the key to check (see class KeyEvent in the Java API)
* @return whether the key, keyCode, was released since the last time we checked*/
public boolean wasKeyReleased(final int keyCode) {
return GameEventUtil.wasInputSourceReleased(aKeyState, keyCode);
} //end wasKeyReleased
//LISTENERS//////////////////////////////////////////////////////////////////////////////
//KeyListener methods
/**Implements the KeyListener interface. Do not call this method directly.
* @param event the KeyEvent that occurred*/
public void keyPressed(final KeyEvent event) {
int keyCode = event.getKeyCode();
if(keyCode < numKeys)
aKeyState[keyCode] = InputSourceState.DOWN;
if(shouldDisableMnemonics)
event.consume();
} //end keyPressed
/**Implements the KeyListener interface. Do not call this method directly.
* @param event the KeyEvent that occurred*/
public void keyReleased(final KeyEvent event) {
int keyCode = event.getKeyCode();
if(keyCode < numKeys)
aKeyState[keyCode] = InputSourceState.RELEASED;
if(shouldDisableMnemonics)
event.consume();
} //end keyReleased
/**Implements the KeyListener interface. Do not call this method directly.
* @param event the KeyEvent that occurred*/
public void keyTyped(final KeyEvent event) {
keyTyped = event.getKeyChar();
if(shouldDisableMnemonics)
event.consume();
} //end keyTyped
//CONSTANTS//////////////////////////////////////////////////////////////////////////////
//keyboard
private static final int numKeys = 600;
//VARIABLES//////////////////////////////////////////////////////////////////////////////
private InputSourceState[] aKeyState = new InputSourceState[numKeys];
private char keyTyped;
private final boolean shouldDisableMnemonics;
} //end class GameKeyListener
I’m using this class with mnemonics disabled.
I added this listener to every Component added to the game so that focus wouldn’t be an issue. It’s a full-screen exclusive mode game.
The problem has only occurred once since I cleared the action map. The focus isn’t going to another process so far as I can tell - the game just minimizes. Normally, when the game minimizes it stops playing, but when it minimizes from pressing escape the game just keeps going. I guess this means the game isn’t receiving the event saying that it’s been minimized.