A
A
Andrew Lays2015-11-08 22:57:21
Python
Andrew Lays, 2015-11-08 22:57:21

How to make SendKeys work via RDP?

There is a remote Windows server, the PROGRAM program is installed on it, Open Server is also installed, with open access from the outside. I installed Open Server in order to run the necessary python scripts on the server simply by knocking on the server IP address from the outside, a php script was written for these purposes, which, when called, launches the necessary python script. So in order to run a python script on the server, I run the server.domain/script.php script. Next, the python script should, for example, launch a notepad and type text in it, for this I use the following python code:

import SendKeys
from pywinauto.application import Application

app = Application().Start('notepad')

SendKeys.SendKeys('some text')

Then, when I go to the server, I see only an open notepad window, empty, without text.
If I restart the same python script on the server through the console, notepad will open and the text "some text" will be printed into it. As a result of googling and testing, I realized that typing and mouse control via RDP do not work for security. I also found some tips to use SendInput and SendMessage, but couldn't apply them to python. How to print text remotely, can remove security restrictions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Ryabov, 2016-05-16
@vasily-v-ryabov

To keep the desktop active, you can use VNC (for example, we use TightVNC as client and server) instead of RDP. Only, if you still logged in via RDP, then you need to restart the server in order for the VNC session to work normally. And the script on the server cannot be run as a service (the active GUI context is not available from the service). For VNC, you may need to update your video driver as well. it displays the real output from the graphics card as opposed to the virtual RDP.
As for pywinauto, there SendKeys just uses the SendInput system function. The UIA branch on github (release scheduled for July) already has a send_chars(...) method that sends WM_CHAR via SendMessage (works for a minimized window and when Windows is locked). But this method must be used for a specific window, not just globally for the system. Example:

app = Application().start('notepad.exe')
app.UntitledNotepad.Edit.send_chars('Hello World!') # не нужен GUI context
app.UntitledNotepad.Edit.type_keys('\nTyping to the active window...', with_newlines=True) # нужен GUI context

Calling a method for a specific control also automatically waits for the window/control to actually appear and be ready for input.

R
Roman Elizarov, 2016-04-23
@FantomNotaBene

make the wait between the line where the application starts and SendKeys()
from time import sleep
...
sleep(3)
...
I think everything works as it should, but, as said above, the text is sent to the current active window, and at the moment when keystrokes are transmitted, the notepad simply does not have time to open and be in focus,
and keystrokes are transmitted, I believe, to an empty desktop, where, of course, they do not cause any reaction to themselves

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question