0

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

SKulibin
  • 719
  • 1
  • 6
  • 14
  • 1
    An application running with user X being able to send keystrokes to applications from user Y would be a major security issue. That should not be possible by design. – João Pinto Apr 22 '16 at 10:15
  • How about running the automation script under user Y? Of course, you need a simple launcher script for that. – Vasily Ryabov Apr 22 '16 at 10:27
  • @João Pinto No, user X run the program and it should be running in the environment of X. Even when the user is changed, all its programs should continue working. For example, if I run the script that calculates something and change the user to another, after relogining I see that the script was working all that time. – SKulibin Apr 22 '16 at 10:30
  • @Vasily Ryabov - what do you mean? – SKulibin Apr 22 '16 at 10:32
  • If you used something like `RunAs` from Python script to run the program under different user, it would be OK to run this pair from another Python script. But if you're calling `Application().connect(...)` it seems that you're trying to send key strokes to another active desktop, but it's not possible. – Vasily Ryabov Apr 22 '16 at 10:48
  • SendKeys.SendKeys does not return any error. It sends keys, but where? – SKulibin Apr 22 '16 at 10:58
  • 1
    It sends keys to the current active desktop. Why should it fail? – Vasily Ryabov Apr 22 '16 at 11:06
  • So it is impossible to tell the program that the active desktop for program that was started with user X is the desktop of user X? – SKulibin Apr 22 '16 at 12:17
  • How is it possible to "explain" to the application what desktop is the main? I am thinking about radmin or teamveawer, for example I login to the desktop of another user, run my application and leave the connected application open. Should it work? Is there another, more easy option to do so? I just want to cooperate my work and the work of application (that need all control over the keyboard) on the same computer. – SKulibin Apr 22 '16 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109903/discussion-between-skulibin-and-vasily-ryabov). – SKulibin Apr 22 '16 at 12:30

1 Answers1

1

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.

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