发送某些键与蟒蛇非活动窗口(Send some keys to inactive window wi

2019-06-18 14:30发布

我试着某些键发送到使用python非活动窗口/进程/ PROGRAMM的(Win32 / 64)。 已经阅读pywinauto和的SendKeys,但寄来键前两者的激活窗口。

有没有什么办法与非活动窗口的工作没有激活它?

如果有人张贴简单的例子/片断这将是巨大的。

谢谢。

Answer 1:

这是一个很老的文章,但还没有来过一个答案,我一直在寻找的东西正是这样,我不得不花6小时通过去#1,并结束了刚刚阅读完所有的C文档,因为它是更加有用。

<python>
#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", "test - 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, 0x44, 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)
</python>

我见过的帖子各地使用

hwndEdit = win32gui.FindWindowEx(hwndMain, hwndChild, "Edit", "test - Notepad");

但我永远无法弄清楚。 此外,关于微软的网站所有的文档都改变暧昧,所以我加了我自己的我是如何理解它。

这应该让你开始,并应成为别人的帮助。 如果其他人有修改让我知道。

Win32的Python库



文章来源: Send some keys to inactive window with python