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.

Track Mouse Movements

Hi

Looking for some advice, I want to be able to record the mouse movement coordinates and record them each time the mouse moves or clicks.

Now my idea is to run a java app in the background and then this will record the coordinates of the mouse when the user is using their programs so I can then use the coordinates to automate mouse movements and simulate clicks to automate tasks so obviously I need to be able to use all programs on the computer but to record the clicks and movements.

I have looked and it seems there is a mouse listener which will achieve this however it seems to need to occur over a java component so I need a way to have a jFrame/Panel invisible and not in the way of the programs I am using so It can record the coordinates when using the other programs.

Re: Track Mouse Movements

Tracking mouse movements is easy enough with MouseInfo. It's a limited solution though because it won't handle click events. A hackish solution to grab click events would be to add a AWTEventListener to monitor system-wide AWT events. It's a problematic solution because if the app looses focus the events won't get picked up.

Another, even more hackish solution would be to put a windowless, transparent JFrame over the entire screen and use that to monitor click/mouse moves. When a click even occurs, minimize the transparent window, use a Robot so that whatever is under the JFrame recieves the input then re-maximize the window. It will be more reliable than an AWTEventListener but jam packed full of gotcha's.

Re: Track Mouse Movements

Thanks, looks like I could use this, just need to find a way so I can manipulate the application underneath whilst a semi-transparent jframe is on the whole screen. I tried the Robot as suggested and it works but there are lots of gotchas like you have mentioned so I guess it is just the case of working through them and finding a solution to each one when they turn up.