简体   繁体   English

Java-将鼠标事件发送到Applet

[英]Java - Send mouse events to Applet

I'm trying to make a program which can send mouse input to a Runescape applet. 我正在尝试制作一个可以将鼠标输入发送到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. 我正在为Runescape制作“ Twitch Plays Pokemon”程序,该程序已被确认是允许的。

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. 无论如何,我已经创建了一个加载器,该加载器将从网站上提取游戏罐并在JFrame中打开它,这意味着我有一个包含游戏的Applet实例。 I need to somehow dispatch mouse events to this applet. 我需要以某种方式将鼠标事件调度到此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. 我希望能够发送鼠标悬停事件以及右键单击事件。

You can probably do this with the java.awt.Robot class. 您可能可以使用java.awt.Robot类进行此操作。 I've never done it but it seems like it would work. 我从来没有做过,但似乎可行。

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. 这里要注意的是applet.getComponent(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. 您可以使用JNI和Windows API(假设这一切都在Windows上运行,其他平台可能具有类似的推论)将模拟的鼠标事件发送到该窗口。

You can use Spy++ to monitor the messages being sent to that window. 您可以使用Spy++监视正在发送到该窗口的消息。 You can use FindWindow to get the window's hWnd , and then use SendMessage or PostMessage to send the simulated mouse events. 您可以使用FindWindow获取窗口的hWnd ,然后使用SendMessagePostMessage发送模拟的鼠标事件。

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM