The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Virtual Cursor?

Imagine this: You make a simple game using a contained within a JFrame. The game is played by moving your mouse around and clicking on different things.

Can you then make a virtual mouse to interact with that game that will not interfere with your normal cursor actions, and only do things within the JFrame. Like you could browse the web while it is running and it wouldn't interfere.

Note: This has nothing to do with interacting with the game objects in code directly, but simple a virtual mouse overlay to interact with it in a simulated way.

Re: Virtual Cursor?

What is supposed to make the virtual mouse cursor move? You mention that you could be using the real mouse for something else like browsing. Not that it really affects your question, but I was wondering. Will it be following some logic of its own? or responding to other events like keystrokes?

In any case I don't think you can rely on the OS to help you. What I mean is that the real mouse will be interacting with the web browser or whatever and, as far as the OS is concerned, that is what your mouse is doing. Your game application will simply not receive any mouse event information.

Your game will, therefore, have to be prepared to interact with events generated by the virtual mouse. So perhaps "this has nothing to do with interacting with the game objects in code directly" needs to be clarified. Surely everything that affects the game's state arises because of interactions with the game objects reflected in the code of the game, those objects and whatever it is that initiates the interactions.

There may be some way of programatically sending the game application mouse or keyboard input even while some other application has focus. (and without that application permanently losing focus) But that would seem to be some OS specific question and, as such, not one really suited to finding an implementation in Java.

Re: Virtual Cursor?

Sorry if I was not clear enough. The cursor would be pre-programmed similar to a macro to do a series of events. But the code running that cursor will not be able to interact with, or modify, any of the code of the game. Almost like a overlay the size of the JFrame that can imitate clicks to for objects below it, but it would be virtually controlled with pre determined actions, if that makes sense.

--- Update ---

Also I think you might be able to do with MouseEvents that are created with the MouseEvent(), but I don't know how to send them to a component or get the component to act upon that MouseEvent.

--- Update ---

In Other word this is a Virtual Robot that will no take control of the mouse. And it would be beneficial if it could run without focus.

Re: Cursor emulation in java

Oh, I'm really sorry, man, haven't read your post patiently. Then most likely you'll have to use JNI. There's some library, take a look. Even if that's only for listening, you can combine it in an appropriate way with Robot, so that each time a user moves mouse the event is intercepted by the lib and the mouse is forced to stay on it's position with Robot.