Giving input to a GUI application through another

2019-09-18 09:36发布

问题:

What i want to know is how can you give input to a GUI app that is closed source and does not have any public API.

To be more concise, let's say you open solitaire and want through a program to play it. Or, to go even to the basics, you have an GUI app with a button and you want to click it through another program.

I know the question is a little vague, but that's the best I can phrase it. Please help me with some edits or some comments to make it more specific.

回答1:

Investigate SendInput(). It can be used to simulate mouse movements and key presses.

To locate a Windows application using its GUI you can use EnumWindows() to locate a Window with a particular title. This will provide a Window handle. To give that window focus, you could:

  • obtain the window handle via EnumWindows()
  • use GetWindowRect() to get the co-ordinates of the window rectangle
  • move the mouse to within the bounds of the window using SendInput() and simulate a mouse click using SendInput()

I have done this once, and it is infuriatingly difficult to get right. Once you start your program sit on your hands: don't touch the mouse or the keyboard.

(I have no knowledge on how to do anything like this on Linux)



回答2:

Assuming its an X11 app in Linux you could connect the process to one end of a named pipe and then echo X-Input events down the other end of the pipe.