使用python将一些键发送到非活动窗口 [英] Send some keys to inactive window with python

查看:356
本文介绍了使用python将一些键发送到非活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python将一些密钥发送到不活动的窗口/进程/程序(win32/64).已经阅读了有关pywinauto和SendKeys的信息,但是它们都在sendin键之前激活了窗口.

I'm tryin to send some keys to inactive window/process/programm (win32/64) using python. Already read about pywinauto and SendKeys, but both of them activate window before sendin keys.

有什么方法可以在不激活窗口的情况下将其激活吗?

Is there any way to work with inactive window without activate it?

如果有人发布简单的示例/摘要,那将是很好的.

It would be great if someone post simple example/snippet.

谢谢.

推荐答案

这是一篇很老的文章,但是这里没有答案,我一直在寻找类似的东西,并且我花了6个小时来学习Stackoverflow ,最后只阅读了所有C文档,因为它更有用.

This is a really old post but there has not been an answer here, I was looking for something exactly like this, and I had spend 6 hours going through Stackoverflow, and ended up just reading all the C documentation because it was more useful.

<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>

我看到了所有可以使用的帖子

I've seen posts all over to use

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

但是我永远无法弄清楚.除了Microsoft网站上的所有文档含糊不清之外,所以我添加了自己的理解方式.

but I could never figure it out. In addition to that all documentation on Microsoft's site is vary ambiguous, So I've added my own of how I understand it.

这应该可以帮助您入门,并且应该对其他人有所帮助.如果其他人有修订,请告诉我.

That should get you started and should be helpful for others. If anyone else had revisions let me know.

Win32 Python库

这篇关于使用python将一些键发送到非活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆