0

I'm using the following code to send keystrokes to an application:

import win32com.client
import time
process_id = 2032 # I change this value if needed
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate(process_id)
shell.SendKeys('^o')
time.sleep(.5)
# more input ...

The problem is: at some point everything goes downhill, meaning: the application is hanging (unpredictable for how long and when!) for whatever reason with unpleasant consequences. For example instead of clicking the "OK" button via the {ENTER} command, that keystroke does something else. After that all the keystrokes mess things up because they do not do what they were intended to do. Is there a difference between using time.sleep and win32api.sleep by the way? And is there a systematic way to check whether the application that I'm sending the keystrokes to, is in a stable ready-state again?

3
  • The functions of time.sleep and win32api.sleep are the same, except that time.sleep is in seconds and win32api.sleep is in milliseconds. Commented Sep 27, 2019 at 10:03
  • Seems pointless to use sleep at all Commented Sep 28, 2019 at 13:33
  • @David: it does help. I can use a sleep delay which gives the program the time it needs to open a file or make edits or save a modified file. I've had scenarios where it completely breaks without the delays. But instead of guessing which delay can handle any circumstances, I would prefer a more systematic solution. Commented Sep 30, 2019 at 7:33

1 Answer 1

0

If the application is developed by you, you can use SendMessageTimeout to check the response of the app window. If the response times out, the application is considered to be hang on(not on ready-state).

Sign up to request clarification or add additional context in comments.

5 Comments

If the application was developed by me I would not need to send keystrokes to it ;-) I'm trying to execute a program thousands of times (once for every file) and the program doesn't have an API nor can it be executed via the command line.
Based on this answer and document, does Process.Responding Property help?
Saw the last comment just now: looks like Process.Responding is not available in pywin32. I have searched for "responding" in the documentation but I only find 2 items which seem to be unrelated :-/
I'm trying to find a way to port my code to C#, so that I can test Process.Responding, but it doesn't look like in C# it is as simple as shell = win32com.client.Dispatch("WScript.Shell")
I think I found (part of) the answer. It looks like I'm using AppActivate too much. Using AppActivate shortly before SendKeys seems to mess things up (swallowing letters etc). So what I do now instead is to check whether my app still has focus. If it doesn't, I refocus and sleep for 2 seconds before sending keys (or calling AppActivate again in case focus got lost again during the sleep).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.