I am trying to become more familiar with programming GUI applications on Windows that interface with the OS. As a starting project, I want to build an App similar to Grammarly. The app should do the following:
- Monitor text input in any active textbox
- When triggered (either automatically or via a keyboard shortcut), it will send the contents of the current textbox to a backend
- Run it by the GPT API to correct mistakes
- Insert the corrected text back into the active textbox
Steps 1 and 2 work fine, I am using pywinauto.Desktop(backend="uia").window().descendants()
to find the descendant that has_keyboard_focus()
and extract the text.
However, I am having trouble with re-inserting it. Replacing the text using pywinauto
did not work in all applications, so currently I am just sending ctrl+a and ctrl+v. This is unreliable as it takes time and sometimes ctrl+a gets sent too late, resulting in duplicate text.
Also, in the future, I want to use this, e.g., in Word, and it should not always replace the whole document, but only the current paragraph or the current selection. What is a better way to insert text back into Windows text controls?