search

Home  >  Q&A  >  body text

python3.x - 如何用python激活指定窗口的输入框,方便下一步模拟输出?

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

PHPzPHPz2802 days ago1702

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师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_KEYDOWNWM_KEYUP,例如你想展现用输入法打字的效果,或者你想调用某个组合键。

    关于窗口句柄的获取,Win32里面把控件之类的也称作Window,但是FindWindow()只能查找顶层窗口(也就是狭义的“窗口”),如果需要查找子窗口(也就是通常所说的“控件”),需要用FindWindowEx()

    Regarding obtaining the window handle, Win32 also calls controls and the like 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)

    The effect of running the sample program is: Notepad does not gain focus from beginning to end, but a string is inserted at the cursor.

    In this application scenario, simulating keyboard strokes is not a good choice - the input result depends entirely on your keyboard layout/input method. Although the US QWERTY keyboard is commonly used in our country, and the key codes basically correspond to English characters, directly passing the characters through the window message can easily ensure that the input results are completely consistent with the requirements, so why not do it.


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

    There are many books on MFC development in China, which generally explain the basic knowledge of Win32. However, the title of the book usually does not mention MFC and Win32, but the name Visual C++, which is also drunk...


    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.

    VB/.NET/Delphi also has much more relevant information than Python. As a cross-platform environment derived from the community, Win32 development is not the main battlefield for Python. 🎜

    reply
    0
  • ringa_lee

    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/

    reply
    0
  • Cancelreply