So I have been struggling with this in about a week now, and spent well over 6 hours on it without luck.
Short version of what I'm trying to do: I want to be able to send a virtual keystroke and or mouse click to an specific nonvisible/minimized program running on my computer.
Sounded like an easy task in my head... I was wrong.
I have tried many different things, and searched for all I can come up with. Some examples of the code I've tried (primarily taken from other posts talking about this topic):
#you will need the win32 libraries for this snippet of code to work, Links below
import win32gui
import win32con
import win32api
from time import sleep
#[hwnd] No matter what people tell you, this is the handle meaning unique ID,
#["Notepad"] This is the application main/parent name, an easy way to check for examples is in Task Manager
#["test - Notepad"] This is the application sub/child name, an easy way to check for examples is in Task Manager clicking dropdown arrow
#hwndMain = win32gui.FindWindow("Notepad", "test - Notepad") this returns the main/parent Unique ID
hwndMain = win32gui.FindWindow("Notepad", "Untitled - Notepad")
#["hwndMain"] this is the main/parent Unique ID used to get the sub/child Unique ID
#[win32con.GW_CHILD] I havent tested it full, but this DOES get a sub/child Unique ID, if there are multiple you'd have too loop through it, or look for other documention, or i may edit this at some point ;)
#hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD) this returns the sub/child Unique ID
hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD)
#print(hwndMain) #you can use this to see main/parent Unique ID
#print(hwndChild) #you can use this to see sub/child Unique ID
#While(True) Will always run and continue to run indefinitely
while(True):
#[hwndChild] this is the Unique ID of the sub/child application/proccess
#[win32con.WM_CHAR] This sets what PostMessage Expects for input theres KeyDown and KeyUp as well
#[0x44] hex code for D
#[0]No clue, good luck!
#temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0) returns key sent
temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x37, 0)
#print(temp) prints the returned value of temp, into the console
print(temp)
#sleep(1) this waits 1 second before looping through again
sleep(1)
This is the one with most success, but when I tried it in another program (Calculator), it didn't seem to work.
import win32gui
import win32con
import win32api
def click(x, y):
hWnd = win32gui.FindWindow(None, "Lommeregner")
lParam = win32api.MAKELONG(x, y)
hWnd1= win32gui.FindWindowEx(hWnd, None, None, None)
win32gui.SendMessage(hWnd1, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
win32gui.SendMessage(hWnd1, win32con.WM_LBUTTONUP, None, lParam)
click(200,1500)
("Lommeregner" is just my language's word for calculator). This was a try for a mouse press in another program, but it didn't work either
I've tried a lot of different things, which people said should work. I don't know if I'm just doing it wrong or something, but I would be grateful for any help I can get.
So to recap: I would like to send/simulate a keypress/mouse click in another application/window, without it being in front, or being pushed to front. I have tried a lot of different things, but without much luck. I'm quite new to the programming scene, and this website, so sorry if this post was a bit confusing. Feel free to ask questions or tell me if I need to post some specific information:)