6

I have tried using python libraries: pyautogui + pwinauto. But to no avail. Once the window is minimized the text is no longer send.

code snippet:

import pyautogui
import time
pyautogui.hotkey('win')
time.sleep(1)
pyautogui.typewrite('notepad')
pyautogui.hotkey('enter')
time.sleep(2)
pyautogui.typewrite('test aaaaaaaaaaaaaa bbbbbbbbbbbb cccccccccc ')
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
george
  • 685
  • 2
  • 9
  • 22
  • According to the [pyautogui docs](https://pyautogui.readthedocs.org/en/latest/cheatsheet.html#keyboard-functions) you can't do this: "Key presses go to wherever the keyboard cursor is at function-calling time." – Steven Rumbalski Oct 08 '15 at 15:33
  • Which one? Your program's window or the target window? – cdonts Oct 08 '15 at 15:45
  • target window, in this case 'notepad'. – george Oct 09 '15 at 07:04
  • The answer looks correct and still not accepted. Sorry to remind about such old thread. Just updated the answer for latest pywinauto version. – Vasily Ryabov Jan 20 '19 at 22:01

1 Answers1

7

pywinauto can send text to a minimized window.

from pywinauto import Application
app = Application(backend="win32").start('notepad.exe')
app.UntitledNotepad.minimize()
app.UntitledNotepad.Edit.set_text('some text\nsecond line')

type_keys() method requires a control to be in focus. But set_text sends WM_SETTEXT message by window handle, so focus is not required.

Another example of script dealing with minimized window: Python - Control window with pywinauto while the window is minimized or hidden

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78