preventing the mouse from switching apps

I am working on this is the project:
http://code.google.com/p/pg3b/
When activated, the user will be moving the mouse, clicking, and pressing buttons on the keyboard. Until you press a special hotkey to deactivate it, the app uses these inputs to control the PG3B, which controls the Xbox controller. This lets you play FPS and other games on the Xbox 360 using the keyboard, mouse, or any other computer peripheral such as joysticks, pedals, etc.

When activated, I can easily prevent the app from accepting keyboard input or mouse clicks:

// Stop keyboard buttons.
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
	public boolean dispatchKeyEvent (KeyEvent event) {
		return true;
	}
});
// Stop mouse buttons.
getGlassPane().setVisible(true);

However, I need to prevent the mouse from clicking on other windows, which would cause the app to lose focus and then it would no longer be handling the peripheral input. Any ideas on how to solve this? I’d like it to be as cross platform as possible.

Here is a bit of a hack:

// Stop mouse movement.
final Robot robot = new Robot();
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
	public void eventDispatched (AWTEvent event) {
		robot.mouseMove(getX() + getWidth() / 2, getY() + getHeight() / 2);
	}
}, AWTEvent.MOUSE_MOTION_EVENT_MASK);

This isn’t good enough though. If you move the mouse and click fast enough, you are able to click on other windows.

One idea I had was to use LWJGL to grab the mouse. I played around with this but haven’t been able to get it to work yet. My app doesn’t use OpenGL, so I’m not sure what is the minimum required to get mouse grab to work.

Take a screenshot and go fullscreen, displaying that screenshot.

Hehe, that is a pretty cool idea! Wish I could get rid of Robot though (to take the screenshot in this case, not to mess with the mouse).

A possilble improvement on your mouse movement code could be a thread that spins setting the mouse to the centre of the app (plus a small sleep). I’m just thinking that it might set the mouses location a bit faster then waiting on the event to be fired.

In regards to your existing solution; when you also say it’s possible to click outside the app, how possible is this? Is it possible in practice, or just possible if you put your mind to it? You might find that it’s good enough.

I originally tried just a thread with a sleep but found the event to be better. Good point though, I will try doing both and seeing if it helps. It is good enough for now, I’ll probably revisit the issue later.

I mean it is possible because I did it. With a window of about 600x700, I was able to rip on the mouse and click before it could be moved back. It would probably happen very rarely though, and a maximized window would make it nearly impossible at any decent resolution. Mouse sensitivity could make it easier though.

Why?

You should take a look at JInput. It will be able to poll the keyboard and mouse whether or not your app has focus. That’s what I had to use for this, as the idea was that you could record keystrokes within any program, so obviously that makes focus a problem if you’re trying to use KeyListener.

Quote from that page:
I found that it was incredibly difficult for a Java program to find the mouse coordinates on the screen without using a window first

What about:
java.awt.MouseInfo.getPointerInfo().getLocation()

Woah! Does that work regardless of focus? That would be amazing. I was trying to use only JInput to do it, but that only gives you a fairly arbitrary change in value (it isn’t even a pixel change) - it doesn’t give you a location. Although I remember endolf telling me there was a way if I enabled some plugin or another, I tried to find it but couldn’t, and/or couldn’t get it working.

Your suggestion seems like a breeze.

Just try. Would take less time than waiting for this answer: it works regardless of focus