Search code examples
pythonwindowssendkeyspywinauto

Python - control application started by the different user in Windows


I am making application that controls a browser with SendKeys. But as SendKeys get the full control over the keyboard, I want to run this app under the different user. This way I will be working, the application will do what it have to do, and we will not make problems for each other).

The simplest code is

import time
import SendKeys

time.sleep(10)
SendKeys.SendKeys('hello')

I run it, focus on the field where I want to insert my text "hello", and wait. If I don't change the user, all is done as expected. But when I run it, change the user and return after 10 seconds, I see that SendKeys sent nothing to the program.

How to send keystrokes to the program under the different user?

(I was trying to do the same with pywinauto, but the result was almost the same - all is good if I don't change the user, and error if I change it. So I thought that it is much simplier to resolve this problem with only SendKeys).


Solution

  • Just to summarize our discussion in comments and in the chat. Your wishes are very wide. I'm just trying to show you some directions to learn.

    If you want to use SendKeys/TypeKeys/ClickInput methods (working as a real user), you need to run your automation script in the remote session, not locally. This is explained in details in my other answer: SetCursorPos fail with "the parameter is incorrect" after rdp session terminated.

    If you want to run the automation on the same machine silently (in minimized state), there is an example for dealing with hidden windows: Python - Control window with pywinauto while the window is minimized or hidden. Just minimize the window and use silent methods (almost all except ClickInput and TypeKeys).

    Feel free to ask more detailed questions about pywinauto and GUI automation.