Answer the question
In order to leave comments, you need to log in
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')
Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question