Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

bots - How can a program control another program?

Bots, how do they work? Do they tell the video game a key was pressed or the mouse was clicked?

If not is there a way to have your program tell another program a key was pressed? I would like to make a program to beat some game. So any resources or examples are appreciated.

Update: So one way is to emulate keystrokes, so what are some methods to do this (in any language)?

question from:https://stackoverflow.com/questions/1032489/how-can-a-program-control-another-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've written a bunch of bots at one time or another (from Pogo games to Yohoho Puzzle Pirates). For windows, you're usually going to either be sending Win32 events to simulate mouse movements, or spoof the actually low-level messages sent between windows when the mouse is actually clicked. A lot of it really depends on how the program reacts (by accepting the message with the coordinates, or, in Java's case, immediately reading the mouse coordinates). The "automation" part usually involves reading the screen and writing heuristics or algorithms for determining the state, but can also be as nice as packet sniffing (a lot of information there in poor poker implementations) or as hacky as reading memory locations directly. Pretty big "field", and poorly documented as it's pretty profitable and not hard to get into.

Sending Input

C/C++ (in Windows)

For keys, try CodeProject:

http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx

And messages:

http://www.codeproject.com/KB/threads/sendmsg.aspx

Your best bet is to learn to send messages using the Win32 API, then use something like Spy++ or its derivatives to "reverse engineer" how KeyPresses and mouse movements are sent to the window.

Java

Java has an amazingly portable Robot class that is able to:

  1. Read Pixels from the screen.
  2. Control the mouse.
  3. Send keys.

I'd give that a shot if you're looking for quick and easy.

Basic Logic

This is described elsewhere on the internet in depth, but most bots follow a simple state-machine program flow. You read the screen (or packets, or memory), find out what "state" you're in based on your readings and past data, do calculations, and send the result back out to the program.

Reading the screen can be difficult, but can be made easier if you consider that a lot of times, there are a few "lucky" pixels relative to the window that will give you an idea of what state the program is in. The process of finding these pixels can be automated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...