python3.4
(对win的这种窗口啥的并不熟悉。。刚开始接触。。有些术语可能不准确 见谅)
尝试模拟键盘输入,希望可以自动定位到程序的中某个输入框中,并激活,开始输入。
已经尝试用FindWindow找到窗口的句柄,但似乎并不能直接使其成为当前的工作的那个窗口。我用的一个记事本尝试,
wrHd=win32gui.FindWindow('Notepad','write.txt - 记事本')
win32gui.ShowWindow(wrHd,win32con.SW_SHOWNORMAL)
SendInput()
其中SW_SHOWNORMAL等其他几个常数我都试过了,似乎只有在窗口本身已经最小化的时候才可以把窗口激活?,如果没有最小化的话似乎这个showWindow并没有作用。
啊刚才尝试了用SetForegroundWindow()这个函数似乎可以激活窗口。
所以现在一个窗口如果有多个输入框。。求问如何定位某个特定的框呢
总的来说目标就是希望能完成自动定位,输入,这样。
十分感谢!
PS:如果有系统介绍这方面的书籍或者网站等,比如介绍各个api有啥用的(中文最好,因为不想翻msdn...)请一并推荐上来吧~十分感谢!x2
天蓬老师2017-04-17 17:20:44
First of all, you need to clarify your needs. The requirement of "simulating keyboard input" is a bit general, because there are too many ways to simulate input on Win32, each with different effects.
Secondly, moving focus to the text box is not necessary. If you just want to enter a string into a text box, you don't even need to know how to simulate keyboard actions, because Win32 provides a higher level of abstraction WM_CHAR
. In other cases, you may want to manually simulate keyboard keystrokes WM_KEYDOWN
and WM_KEYUP
. For example, you want to show the effect of typing using an input method, or you want to call a certain Key combination. WM_CHAR
。另外一些情况下,你可能希望手动模拟键盘的击键动作WM_KEYDOWN
和WM_KEYUP
,例如你想展现用输入法打字的效果,或者你想调用某个组合键。
关于窗口句柄的获取,Win32里面把控件之类的也称作Window
,但是FindWindow()
只能查找顶层窗口(也就是狭义的“窗口”),如果需要查找子窗口(也就是通常所说的“控件”),需要用FindWindowEx()
Window
, but FindWindow()
can only find the top-level window (that is, a "window" in a narrow sense) ), if you need to find a subwindow (also commonly known as a "control"), you need to use FindWindowEx()
. Let’s take Notepad as an example:
from ctypes import windll as win32
WM_CHAR = 0x0102
try:
hWnd = win32.user32.FindWindowW('Notepad', None)
assert hWnd
hEdit = win32.user32.FindWindowExW(hWnd, None, 'Edit', None)
assert hEdit
except AssertionError:
print('Notepad not found')
else:
for char in 'Hello, 世界':
win32.user32.SendMessageW(hEdit, WM_CHAR, ord(char), None)
I have never studied Win32 systematically. My personal habit is to look at sample codes made by others, and check MSDN if I don’t understand...
No matter what the information is, it is usually for the C/C++ environment. Win32 is for C/C++, and Python is just encapsulated. It is recommended to learn on the C/C++ platform, which will be much more convenient.
ringa_lee2017-04-17 17:20:44
Using pure Windows API to control windows is indeed very flexible, but it requires certain background knowledge. It is recommended that you find a packaged automation operation library online, as I know: http://pywinauto.github.io/