用于在线游戏的 Java 机器人
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12223540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java bot for an online game
提问by George Artemiou
I am creating a bot in java using the java.awt.Robot
. The bot works fine on a browser (I have also tested it using Microsoft Word!) but when I run it in the game, the only function that works is the mouseMove
. I want to build a bot that simply presses keyboard buttons for me.
我正在使用java.awt.Robot
. 该机器人在浏览器上运行良好(我也使用 Microsoft Word 对其进行了测试!)但是当我在游戏中运行它时,唯一有效的功能是mouseMove
. 我想构建一个机器人,只需为我按下键盘按钮。
I instantiate the robot class
我实例化机器人类
Robot r = new Robot();
Then I do some simple stuff: press z,press 1, move the mouse and right click.
然后我做一些简单的事情:按z,按1,移动鼠标并右键单击。
r.keyPress(KeyEvent.VK_Z);
r.keyRelease(KeyEvent.VK_Z);
r.keyPress(KeyEvent.VK_1);
System.out.println("Press 1 button");
r.keyRelease(KeyEvent.VK_1);
System.out.println("Release 1 button");
r.delay(1000);
System.out.println("Move mouse");
r.mouseMove(110, 690);
System.out.println("Press");
r.mousePress(InputEvent.BUTTON3_MASK);
System.out.println("Release");
r.mouseRelease(InputEvent.BUTTON3_MASK);
Why is this happening? Can this Robot class perform these kind of actions within a game if it runs in the background?
为什么会这样?如果这个 Robot 类在后台运行,它能否在游戏中执行此类操作?
Thank you
谢谢
Update: If I run my bot on PES 2012 for example, it works fine but if I run it on an online game like Cabal, it does not work? the protection system of the game does not detect anything so that is not the case.
更新:例如,如果我在 PES 2012 上运行我的机器人,它运行良好,但如果我在像 Cabal 这样的在线游戏上运行它,它就不起作用了?游戏的保护系统没有检测到任何东西,所以情况并非如此。
采纳答案by bruno
First of all, most games have bot protection, so make sure to add a delay to the bot and, maybe, a 'cooldown'. Before that r.delay(1000)
statement, the bot did two instant actions.
首先,大多数游戏都有机器人保护功能,因此请确保为机器人添加延迟,也许还有“冷却时间”。在此之前,r.delay(1000)
机器人做了两个即时动作。
I'm almost sure it's not working because the keystrokes are way too fast: they press and release instantly. Try adding bot.delay(500)
(or more, depends on the game) right after you instantiate Robot class; before all the key pressing functions. That would add a 500ms delay between ALL actions done by the robot.
我几乎可以肯定它不起作用,因为按键太快了:它们立即按下并释放。bot.delay(500)
在实例化 Robot 类后立即尝试添加(或更多,取决于游戏);在所有按键功能之前。这将在机器人完成的所有操作之间增加 500 毫秒的延迟。
public static void doStuff() {
Robot r = new Robot();
r.delay(500); //Or more - depends on the game
r.keyPress(KeyEvent.VK_Z);
r.keyRelease(KeyEvent.VK_Z);
r.keyPress(KeyEvent.VK_1);
System.out.println("Press 1 button");
r.keyRelease(KeyEvent.VK_1);
System.out.println("Release 1 button");
r.delay(1000);
System.out.println("Move mouse");
r.mouseMove(110, 690);
System.out.println("Press");
r.mousePress(InputEvent.BUTTON3_MASK);
System.out.println("Release");
r.mouseRelease(InputEvent.BUTTON3_MASK);
}
I think the only reason why the Z and 1 keys didn't work was the speed everything was done. The game probably has an anti-bot system.
我认为 Z 和 1 键不起作用的唯一原因是一切都完成的速度。游戏可能有一个反机器人系统。
回答by CoasterChris
It depends greatly on what type of game it is. If the code is simply emulating system input like keyboard actions. It should just look like a regular person.
这在很大程度上取决于它是什么类型的游戏。如果代码只是模拟系统输入,如键盘操作。它应该看起来像一个普通人。
However from what it looks like. From your example. Its running at lightning speed thus its prob not detecting the input at all, and/or the anti bot measures on so called game you are trying to bot. Is blocking input. Put delays into the mix. See if that helps. Ill be back for more help. I'm not professional on this. But its my best guess.
然而从它的样子来看。从你的例子。它以闪电般的速度运行,因此它可能根本没有检测到输入,和/或反机器人测量您试图机器人的所谓游戏。正在阻塞输入。将延迟放入混合中。看看是否有帮助。我会回来寻求更多帮助。我在这方面不专业。但这是我最好的猜测。
EDIT:
编辑:
When I mean delay put a delay before the key up events to fire.. That way it has time to process the keys.
当我的意思是延迟在关键事件触发之前放置一个延迟..这样它就有时间处理密钥。