Reputation: 117
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.
Upvotes: 0
Views: 1294
Reputation: 117
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.
Upvotes: 3
Reputation: 9108
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
Upvotes: 0
Reputation: 5451
You can probably do this with the java.awt.Robot
class. I've never done it but it seems like it would work.
Upvotes: 2