I've been playing with the Robot class recently and I have it doing what I want, but I haven't figured out how to interrupt/stop its actions via user input.
For example: I want it to click the desktop a hundred times but I decide forty clicks in that I want to make it quit (or pause).
I'd like to be able to do something simple like press a certain key or press the middle mouse button in order to tell it to stop. In order to do this, it must be able to listen for input outside of the Java application, since the actions the Robot is performing are in other programs.
As edward said, there doesn't seem to be a way to do exactly what I was looking for. So this answer is to explain how I achieved an acceptable substitute.
The other question edward linked to had stated that
MouseInfo.getPointerInfo().getLocation()
is capable of fetching the mouse coordinates regardless of what the mouse is doing. My program uses the robot class to control the mouse within a specific range of coordinates. I also wanted to be able to disable the program via user input.
To achieve this result, I compared the x and y coordinates of the mouse to the x and y coordinates that the robot last set it to. If the two do not match, the program exits.
Pausing the program by this method would be impractical because resuming would require going back to the original x and y coordinate prior to pausing, but it does at least give an example of how to achieve stopping without actually being focused on the java parent program.
In order to pause the program, you would instead compare the coordinates to a range of coordinates (have the coordinates create an imaginary, 2D box). If the mouse's coordinates are within that range: pause. To resume, do the opposite check (mouse not being in that range).
You may be able to use some of the code from this answer:
Simulate a key held down in Java
And then add a Listener to whatever action, component, whatever to call the stop method on the robot command.
Does that provide you with some inspiration?
Edit After further discussion the real question is:
How to respond to external Mouse Events (Outside the Java Application) inside a Java application?
It Seems that you can't without native code and Mouse Hooks as it's OS Dependent.
For Further Discussion see Is it possible to have a MouseMotionListener listen to all system mouse motion events?
Related
I want to make a program that recognize a button that is in the League of Legends client, and when the queue's button shows up the mouse instantly click on it.
Its a sort of a AutoAccept Client...
There is already a program that someone did, but it has so many unnecessary things and it does not work very well....
How do I do to recognize a element inside a program to make my mouse move there when it shows up?
I can try to code this in any language, just gimme a tip pls
You can create a program that takes a screenshot every 100ms, use a recognition algorithm of your choice and then (optionally) performs a mouse click. There are enough libraries that can screenshot for you, you should be able to recognise the button by colour and location and every language should have some method of controlling the mouse.
Sikulix (can be paired with python) is a perfect tool to use for this. It scans over your screen constantly, and if the image in question pops up (the accept button) you can click on it.
You will have to take a screenshot and likely store it as .png so the program knows what it is looking for.
Google Sikulix.
I'm fluent in java, and have messed with vb.net, but I prefer java. I wish to make a program that ++'s a variable everytime I click my mouse in a certain coordinate on my screen. Not sure how to record when a mouse click has happened outside of the program's forms.
This can't be done in pure Java but people have written JNI libraries that can capture global mouse and keyboard events. Take a look at
https://github.com/kwhat/jnativehook
In java you can use the Robot Class to move the mouse and fire mouse clicks. While this is cool, it also "hijacks" the users mouse, so you cannot multitask.
What I want to do is make a "Fake" mouse that acts independently of the system's mouse cursor, and lives only inside my java applet. In this sense the applet would think it was being clicked by the mouse in various (x,y) positions (within the applet), however I can do whatever I want with the system mouse and it will not be affected.
I have seen programs that have accomplished this, I just have no idea where to begin. Perhaps I am just using the wrong terminology for this functionality.
Any suggestions on where to look would be much appreciated. -Thanks
What I want to do is make a "Fake" mouse that acts independently of the system's mouse cursor, and lives only inside my java applet.
Create a Runnable FakeMouse class that fires mouse clicks. Tony Depace provided the code, which I'm adding to the answer to help others.
MouseEvent aClick = new MouseEvent(this, MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis(), 0, 10, 10, 1, false);
dispatchEvent(aClick);
Run the FakeMouse class in a thread in your Java applet.
In Java, how can I measure the speed of the mouse movement but disable the actual cursor movement. For example, when someone swipes their mouse across a table, I want to measure how fast he/she swiped the mouse without the cursor actually moving.
I've tried doing this by resetting the mouse position to a specific coordinate (robot class) but the mouse is able to escape when I swipe really fast.
If this is not possible in Java, a C# or C++ solution would be okay.
Thanks
if you really want to block mouse movement events you'd need to do it much lower - in c/c++ at the input filter layer. here's a question to point you in the right direction: Blocking mouse input from a service in Vista
might be possible using autohotkey (which does a lot of the nitty-gritty stuff for you)
It depends on the operating system but for Windows you could try using Input Hooks. I have had to use this before and it works. The solution is a little complex because you have to write a dll which provides a function address which you can insert in to the input hook chain in Windows. This is essentially what autohotkey does, but they give a very nice scripting interface and they do all the heavy lifting of setting up the various dlls and Win32 calls on your behalf. In case you are curious about input hooks: Hooks Overview
I want to write a program which can control my cursor movements depending on the the way I move my finger on the touchpad. I want my program to get the location where the user touched the touchpad, then I want to control mouse movement using my own program. I want do this in Java. Can I do this in Java? I would like to run it on Windows OS. Does my laptops touchpad device driver provide some API by which I can get the info about when and where the user touched the touchpad?
Think about this situation.
Person wants to exit your program. They touch the trackpad. Your program moves the cursor somewhere they didn't expect. They're upset and confused.
They continue to touch the trackpad, your program continues to do something they didn't expect. They find they're unable to control the cursor. Now what?
Generally, having your program move the cursor is a recipe for disaster.
The cursor is hard enough to spot on the screen. X-windows applications which do "cursor warping" to dialog boxes have an option to disable this because it is confusing.
Removing control of the cursor from the user makes the computer (already very hard to use) much harder to use because there's this "mode" thing. When your program is running, one thing happens. When your program is not running, something different happens.
Look at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Cursor.html
There do not appear to be any methods to change the cursor's position. It tracks the mouse.
However, look at http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Robot.html
This has the ability to synthesize mouse events. Feel free to play with it.