Escape Key Minimizing Java Program

I’m working on a full-screen Java 1.6 program on Windows XP service pack 2. Whenever I press escape, sometimes the game minimizes. At first, I thought this was some feature of Eclipse, but it happens even when I’m not running the game from Eclipse.

This is a real problem because escape is supposed to go to the main menu.

Anyone know how to stop this behavior?

I’m wondering why only sometimes? ESC shouldn’t minimise windows anyway… Anything else running on the machine?

Generally, I’m running Eclipse and a text editor called Textpad. There’s been times when I’ve had other things open as well, but I’m sure there’s been times when it occurred and I had nothing running except Norton Antivirus and Spyware Doctor.

[quote=“fletchergames,post:3,topic:29545”]
Intermittent ones are the worst! I can’t think of any circumstances where ESC would do that on XP - I could understand if it was losing focus, but minimising? Harsh! You should check what process takes input focus after the window’s minimised…

This hasn’t happened with me but the only way I can think of it happening is if the ESC key is bound to an action in the AWT. You can easily set an empty binding by doing some method like component.setActionMap(new HashMap()).

Keith

consume keys you dont use.

Note that this advice should only be taken if your keyboard is made of ham.
[/unhelpful]

A) My keyboard is made of the finest of swiss cheeses.
B) What?
C) I don’t use those acutally, just popped into my brain when reading the question.
D) Perhaps his keyboard is made out of ham?

I did this and now it works, though I have no idea why Escape would be mapped to minimization by default. In Windows (at least on my PC), Ctrl-Escape opens/closes the start menu and Alt-Escape cycles through the programs that are open. It seems unlikely that I was using either of those key combinations by accident because neither Ctrl nor Alt is used by my game.

In any case, the problem hasn’t happened since.

I was already consuming all keyboard events, but that apparently doesn’t affect the ActionMap.

The “fix” is still in place, but it isn’t working anymore. The problem just didn’t happen for a while.

I don’t know why the Escape key is minimizing the program, and I don’t know how to fix it.

when I got into that kind of trouble when I couldn’t fix the thing… I just started building game over and try to see after what addon bug arises. Of course this takes lots of time…

There must be an answer to this; Escape key presses don’t normally minimise windows…

Few thoughts…
Can you post your key-capture code? I always use keyPressed() &c. (but that’s applets) - How are you handling keys?
Are you running more than one focus-capturing component? Is the minimising coming from somewhere else?
What takes focus when the window minimises? Is some other process taking focus off you?

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.

Do you have any code to minimize the window in your project? If yes, then you can place a breakpoint there, debug the application and once the breakpoint is reached, check the call stack to find the wrong call. If you don’t have such code, then most likely some utility you have installed (maybe norton) grabs the key on a global level and your code is not the problem.

There’s no code that minimizes the window. So I guess it must be something else. I have several utility programs running most of the time.

does the problem appear on other computers? do other java apps suffer from same problem on your computer?

Key code’s clean, intermittent occurrance, no minimising code within game; sounds like an external utility problem to me too.
You could chuck a system.out.println(keycode) at the top of keyPressed() to check if you actually get the esc keypress when the bug happens…

I’ve never encountered this problem on a different computer and never heard of it happening to anyone ask. I did ask someone who played the game about by email, but he never replied.

I don’t recall ever having this happen on any other program.