D
D
Dmitry2017-09-17 15:18:18
Python
Dmitry, 2017-09-17 15:18:18

How to pass keystrokes to an inactive window?

There is a window with the game and a small script that presses the buttons and after a while clicks the mouse on the coordinates of the game window
how to make these actions be in the background and not interfere with the work on the PC

import pyautogui
import time

while True:
    time.sleep(3)
    pyautogui.keyDown('space')
    time.sleep(1.5)
    pyautogui.keyUp('space')

    time.sleep(0)
    pyautogui.keyDown('space')
    # w=0
    # while w!=30:
    #     pyautogui.keyDown('enter')
    #     time.sleep(0.1)
    #     pyautogui.keyUp('enter')
    #     time.sleep(2)
    #     w+=1
    time.sleep(90)
    pyautogui.keyUp('space')

    x, y = 1068, 950
    pyautogui.moveTo(x, y)
    pyautogui.click()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
felamonpardon, 2020-07-17
@felamonpardon

Hello, a little late, but maybe it will be useful to someone.
I myself puzzled over how to send a click and write any letter in a NOT active window.
The example used a regular notepad.
The code:

import win32gui
import win32api
import win32con
import time
#import pyautogui


def click(x,y):
    hWnd = win32gui.FindWindow("Notepad", None)
    #print(str(hWnd))
    childHWND = win32gui.FindWindowEx(hWnd, None, "Edit", None)
    #print(str(childHWND))
    lParam = win32api.MAKELONG(x, y)
    #клик
    win32api.SendMessage(childHWND, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
    win32api.SendMessage(childHWND, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
    time.sleep(1)
    temp = win32api.PostMessage(childHWND, win32con.WM_CHAR, 0x44, 0) #пишет букву
click(50,50)

D
DENIS SHELESTOV, 2017-09-17
@djdeniro

As a clumsy solution - put the whole thing in a virtual machine.
If this is a browser game:
Easy: rewrite in JS and run from the console
Medium: learn requests and automate in python

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question