Java - Send mouse events to Applet - java

I'm trying to make a program which can send mouse input to a Runescape applet. Before you ask what I want this for, it isn't a bot. I'm making a "Twitch Plays Pokemon" program for Runescape which has been confirmed to be allowed.
Anyway, I have created a loader which will pull the game jar from the website and open it in a JFrame, meaning that I have an Applet instance which contains the game. I need to somehow dispatch mouse events to this applet. I've looked everywhere but whenever I search for this, I just find pages about listening for mouse clicks instead of dispatching them...
I should note that the Robot class isn't what I'm looking for; the mouse actions must be virtual and run within the application. I know this is possible but I'm struggling to find out how it's done.
How can I accomplish this? I want to be able to send mouse hover events as well as right/left click events.

I've found my answer, guys. It was quite simple. This is what I did to perform a mouse click on the applet:
applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
MouseEvent.MOUSE_PRESSED,
System.currentTimeMillis() + 10,
MouseEvent.BUTTON1,
x,y,
0,
false));
applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
MouseEvent.MOUSE_RELEASED,
System.currentTimeMillis() + 10,
MouseEvent.BUTTON1,
x,y,
0,
false));
The thing to note here is the applet.getComponent(0) part which was actually directed at the game canvas.

You can probably do this with the java.awt.Robot class. I've never done it but it seems like it would work.

You could use JNI and the Windows API (assuming this is all running on windows, other platforms probably have similar corollaries) to send simulated mouse events to just that window.
You can use Spy++ to monitor the messages being sent to that window. You can use FindWindow to get the window's hWnd, and then use SendMessage or PostMessage to send the simulated mouse events.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

Related

I want to make a program that recognize a button that is in the League of Legends client

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 am unsure how to record how many times a mouse was clicked in a certain area

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

Make a "Fake" mouse in java?

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.

Listeners in conjunction with Robot class

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?

regain focus after java.awt.Robot.keyPress()/mousePress()

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off

Categories

Resources