python - 将虚拟按住/鼠标单击发送到后台/最小化应用程序
问题描述
所以我在大约一个星期内一直在努力解决这个问题,并且在没有运气的情况下花了 6 个多小时。
我正在尝试做的简短版本: 我希望能够将虚拟击键和/或鼠标点击发送到我计算机上运行的特定不可见/最小化程序。
在我的脑海中听起来像是一件容易的事......我错了。
我尝试了很多不同的东西,并搜索了我能想到的所有东西。我尝试过的一些代码示例(主要取自其他讨论该主题的帖子):
#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)
这是最成功的一个,但是当我在另一个程序(计算器)中尝试它时,它似乎不起作用。
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”只是我的语言中用于计算器的词)。这是在另一个程序中尝试鼠标按下,但它也不起作用
我尝试了很多不同的东西,人们说应该可以。我不知道我是不是做错了什么,但如果我能得到任何帮助,我将不胜感激。
回顾一下:我想在另一个应用程序/窗口中发送/模拟按键/鼠标点击,而不是在前面,或者被推到前面。我尝试了很多不同的东西,但没有太多运气。我对编程场景和这个网站还很陌生,如果这篇文章有点混乱,我很抱歉。如果我需要发布一些具体信息,请随时提出问题或告诉我:)
解决方案
推荐阅读
- java - 用于多个响应的肥皂服务 - 同步和异步
- excel - Excel VBA - 我的循环永远存在 - 想法?
- sqlite - SQLite:如何创建包含 3 个表和 2 个“端点”的 INNER JOIN?
- javascript - 如何更新一个复杂的数组?
- c# - 使用子字符串消除目录路径不起作用
- google-app-engine - 为什么今天的 gcloud 组件更新后 dev_appserver.py 报告“没有这样的文件或目录”
- javascript - 如何在 React 组件中添加属性以便在布局中用作“children.property”?
- go - 如何将多个返回值传递给可变参数函数?
- php - Woocommerce:在 class-wc-product-variable.php 文件中为 foreach() 提供的参数无效
- android - Android getParent() 为 null 但已设置 parentActivityName