Custom Cursor disabled by alt key

I noticed a strange problem that sometimes showed up in my game where the custom cursor would be disabled if the alt key is pressed.
I tracked the problem down to the custom cursor will be disabled when the alt key is pressed if and only if the mouse isn’t moved and no other key is pressed in combination with the alt key.
Thinking this must be a bug in my game, I created a simple test program and was surprised to find the same problem.
I have searched google and JGO and not seen any mention of this problem.
I’m hoping someone knows a workaround or can point me to how I should be doing it different.

Here is the test program.

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


@SuppressWarnings("serial")
public class CustomCursorTest extends JFrame {
	BufferedImage image;
	Cursor cursor;
	
	public CustomCursorTest() {
		setPreferredSize(new Dimension(800, 600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		String url = "http://www.java-gaming.org/Themes/SMF_Revamped_v2c/images/on.gif";
		try {
			image = ImageIO.read( new URL(url) );
			cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(), "Cursor");
			setCursor(cursor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pack();
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(image, 100, 100, null);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CustomCursorTest test = new CustomCursorTest();
		test.setVisible(true);
	}


}

  • edited for clarity.

I found a workaround for now by simply consuming the key event. I can’t help but think this could lead to strange side effects later.


	public void keyPressed(KeyEvent e) {
		if(e.isAltDown()){
			// handle alt events
			e.consume();
		}
	}

Using Frame.setUndecorated(true) might help. Basically pressing alt with nothing else activates the menu system. Even if you don’t have a menu defined there is always the system menu attached to any decorated frame. By removing decorations you remove the system menu. But this also prevents the user from dragging/resizing the frame.

Though you could then implement your own resizing and draggy widgets.

Cas :slight_smile:

Thanks for the replies, unfortunately I have the same problem with undecorated frames.
I didn’t mention it before but a solution that would also work for applets would be ideal.

Following up on the menu’s I found JMenuBar.setCursor(cursor) yet it didn’t help either.
I also tried iterating over all the components returned by JFrame.getComponents() and calling setCursor.

Adding a key listener to consume the key is bothering me less every day.

getMenubar().setEnabled(false) ?

The closest I found was getMenuBar() and I didn’t see a method to disable it.
A JMenuBar returned with getJMenuBar() did have the setEnabled method but calling getJMenuBar().setEnabled(false); didn’t seem to make a difference for this problem.

Both getMenu methods returned null by default and I simply set them with the default constructor for the class.
That may have something to do with it but i don’t plan to use these menu systems so I didn’t peruse it further.

Thanks for the replies, has anyone else run this and had the same problem? I haven’t had a chance to test it on any other computers yet.

In terms of side effects of consuming the event, I wouldn’t worry about it unless you were hoping to have any alt-key defined actually working in your game.And sounds like you don’t.

I do use the alt key but I just consume it after the fact so that fix hasn’t caused any side effects.
It just felt hackish and made me think it’s not “the right way”, it doesn’t really bother me now.