2

I want to send direct input to a (possibly) inactive window in python. So far i have found a solution to send direct input via ctypes and i have a solution to simulate input to a window, which doesn't work with some games as the one i am testing with (GTA V) is using direct input, using postMessage()via py32win.

So how would i now go about simulating those direct inputs while the target window is possibly not active? Is this even possible?

I would think it should be, because AutoHotkey for example can manage to send direct in such a case.

If you would like to experiment yourself i can provide the postMessage() version, otherwise i would like to keep the question code-free.

DJSchaffner
  • 562
  • 7
  • 22
  • I'm not an expert on these things, but I think they would be accomplished through `win32gui`, `win32con` modules right? I know you can find handles for windows like `hwnd = win32gui.FindWindow(None, 'GTA V')` or something similar, and would imagine that once you have the window handle you could just send keys to it? IDK I've never done it before – Reedinationer Mar 27 '19 at 23:14
  • Well that's my problem. I technically got it working the way you said but since GTA V in this case for example expects direct input , it just ignores the keystrokes that are being sent to it via ```postMessage(hwnd, WM_KEYDOWN, wparam, lparam)``` Thus i am looking for a way to simulate direct input, which it expects – DJSchaffner Mar 28 '19 at 01:13
  • Oh I see so this is the same problem I ran into trying to do a project. I'll try (again) to solve it for the both of us :) – Reedinationer Mar 28 '19 at 03:07
  • So you are saying that the game requires scan codes like shown at https://stackoverflow.com/questions/14489013/simulate-python-keypresses-for-controlling-a-game and you have already implemented a solution like this that works for an *active* window and just need to figure a method to send them to an inactive window? – Reedinationer Mar 28 '19 at 06:08
  • Thats correct. I'd like to send those direct inputs to a specific window. Appreciate your help! – DJSchaffner Mar 28 '19 at 12:02

1 Answers1

2

As far as I can tell you cannot do this. A workaround (found at https://www.reddit.com/r/Python/comments/5wpxtt/automation_in_inactiveunfocused_window/) would be

  • get a VM on your machine
  • load the game and your script onto the VM
  • run game and have your script interact with active window (on VM)
  • minimize the VM window of your main machine
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • Mh yes, maybe there is no way to do it. However thats a good workaround you found, unfortunately i believe it's not practical to use because a VM is just too slow for such a game, especially since i would want to actually play when the bot is not active :D Have an upvote – DJSchaffner Mar 29 '19 at 01:43
  • @DJSchaffner I'll take it :) – Reedinationer Mar 29 '19 at 04:55