0

I'd like to use a script to send events such as keyboard or mouse input to automate another program. Think a bot that plays a game for instance (although I don't play video-games).

I want to do this with several (script, program) pairs at the same time.

I known I can automate my mouse and keyboard on windows using autoit or the win32 API, but windows has the concept of "active windows": there is only one active window at a time, and this is the windows that can receive input.

Because of this "active window" concept, I can't automate multiple programs at once.

What alternative do I have?

EDIT: I need to automate the programs without having to wait for another program. Switching the active window using keyboard shortcuts won't do it. Also I want to use the computer while the programs are automated in background.

2
  • 1
    Can you send alt+tab messages using auto it? That maybe the best way to toggle through. Than just ensure the windows are in a predictable order and you should be fine.
    – Ed B
    Commented Sep 21, 2019 at 14:40
  • 1
    If you want to go the win32 route check out the EnumWindows function and SetForegoundWindow. I have never tried to toggle to different applications but this would seem to be the route
    – Ed B
    Commented Sep 21, 2019 at 14:42

1 Answer 1

2

Updated to include alternatives to tying up current user's input

Autoit Using autoit you could do something similar to

Opt("WinTitleMatchMode", 2)

While WinActive('Search')
    Send("{ALT DOWN}")
    Send("{TAB}")
    Send("{ALT UP}")
WEnd

Where ‘search’ contains part of the title for the window.

If you know the order of the window’s focus you could simply send tab enough extra times to get to the given window.

The order for alt+tab is alway in reverse focus order.

Win32 This has been answered on other threads and gets deeper into programing. If you want to go this route you will have to send the windows messages to the process you would like. I would look at: How do I send key strokes to a window without having to activate it using Windows API?

It does not seem to be reliable and would have a lot of pitfalls. Assumptions have to be made about the way keyboard input is handled on the given applications.

Not knowing what you are trying to achieve I may suggest using a virtual machine and allowing that instance to run the macros/scripts. This should allow the user to continue to use the computer while not tying up the desktop the user is using.

3
  • Interesting but too slow, I really need independence
    – Julien__
    Commented Sep 21, 2019 at 16:00
  • What do u mean by independence? If you know tab order than you don’t need to loop. Otherwise win32 maybe your best bet
    – Ed B
    Commented Sep 21, 2019 at 19:08
  • @Julien__ I have updated my answer to try to address your concerns.
    – Ed B
    Commented Sep 22, 2019 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .