0

I'm trying to make a script that runs in the background while I use the main window. It has been done before, and I have seen it. Every time I try this code, it moves the notepad to the front which makes me unable to use the main window properly. Any suggestions? Also, I'm trying to use the mouse to detect something and the print is just as an example.

Code:

from pywinauto.application import Application
import time

# Replace 'Notepad' with the actual executable name or window title
app = Application().connect(path='notepad.exe')

# Reference the window by its title
window = app.window(title='Untitled - Notepad')

# Wait for a few seconds to give the script time to run in the background
time.sleep(5)

# Perform interactions without bringing the window to the foreground
# Example: Type "Hello, world!" into the Notepad
window.type_keys("Hello, world!")

  • Typing keys (or simulating it) will always end up focusing on the window. But indeed, you can change text w/o typing keys. Here is an example with *UI Automation* and C# https://gist.github.com/smourier/b5c3e1574116f50ccdf0e2223a661ee3 I believe `pywinauto` can use UIA as a backend but not sure it exposes the `AutoFocus` property to python code https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomation2-put_autosetfocus – Simon Mourier Aug 26 '23 at 09:45
  • Saying "any suggestions" could open a can of worms on Stack Overflow because it can be seen as subjective. I'd try to edit that out and make the post fit the guidelines. – Blue Robin Aug 27 '23 at 19:39
  • Does this answer your question? [Why does SendMessage not work for some applications?](https://stackoverflow.com/questions/74766562/why-does-sendmessage-not-work-for-some-applications) – Blue Robin Aug 27 '23 at 19:40

0 Answers0