0

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.

Jordan
  • 117
  • 1
  • 12
  • @codeNinja If you're going to take the time to post the snarky comment, why not just post the link to the search result that answers the OP's question? Or at least give the OP a hint for what keywords to use. – Mike B Mar 03 '14 at 20:49
  • @codeNinja If I had found the answer after googling this, do you really think that I would have posted here asking for help and wasting peoples' time? – Jordan Mar 03 '14 at 20:52
  • @Jordan I apologize for my comment, I did not fully understand what you were trying to accomplish and I believed that this question was unfit for this site. I meant no disrespect. That being said I googled "java applet send mouse event without clicking" and got this stackoverflow question that looks like it fits your needs (it says C++ but the accepted answer was given in java). http://stackoverflow.com/questions/10260456/how-to-send-key-and-mouse-events-to-a-java-applet – BitNinja Mar 03 '14 at 21:25

3 Answers3

3

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.

Jordan
  • 117
  • 1
  • 12
2

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

Mike B
  • 5,390
  • 2
  • 23
  • 45
  • The Robot class uses your physical mouse, it does not do what I'm trying to do. Perhaps I should have been clearer but what my program has to do is send 'silent' mouse clicks to the program without actually using the mouse. – Jordan Mar 03 '14 at 20:49
  • You'll probably have to dig in to the Windows API then assuming this will be running on Windows. I did it once before with a Bejeweled bot but I don't have the source anymore. It's pretty straightforward, but ugly. This question seems to have some helpful stuff: http://stackoverflow.com/questions/606820/is-there-a-java-library-to-access-the-native-windows-api – Mike B Mar 03 '14 at 21:37
0

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

Charlie
  • 8,530
  • 2
  • 55
  • 53
  • This sounds very useful actually, since the controller part of the application will be written in C# – Jordan Mar 03 '14 at 21:01
  • Seems like using a hammer to kill a fly -- it would work, but it would make a big mess in the process. There are easier ways. – Kevin Panko Mar 03 '14 at 22:49