Monday, October 26, 2015

Pywinauto Has Been Presented at the Auckland Python Meetup

The regular Auckland Python Meetup has been held on 23 October where Valentin (airelil) presented the project. Unfortunately video wasn't recorded this time. However the slides are still available for everyone. A small insight into Pywinauto internals describing principles of controls name resolution and introductory examples can make your automation scripting a bit easier.

One of the quite popular (strangely) questions, that popped out on the presentation too, is "How can I interact with a console window of cmd.exe?". In this case, Pywinauto couldn't be the answer though. We believe that a GUI automation tool should be used for GUI not a command line. :)

The standard way to send keyboard input to the console application is using subprocess.Popen() object with stdin=subprocess.PIPE and stdout=subprocess.PIPE arguments that are well described in the Python docs.

Interestingly, a person on Stack Overflow discovered that it's possible to send characters to cmd.exe using WM_CHAR window message. It even works silently for a minimized console window. That is could be considered as an alternative for the TypeKeys method in Pywinauto which runs for the active window only. However you have to remember that TypeKeys performs a closer real user behavior emulation than sending of WM_CHAR message or calling SetText for a GUI control.

4 comments:

  1. Hello,
    This may not be the most correct place to post this question, but I haven't found a better one yet. I have used Pywinauto for a couple of projects now, but I am having trouble with one in particular: I am trying to automate a video game emulator, but am unable to send "buttonpresses". I am able to control all of the hotkeys (e.g. Ctrl-O), I am able to control the menu (e.g. File -> Open), but when I try to send the keyboard character mapped to the "a" button for example, nothing happens. I've tried TypeKeys/SendKeys and its various parameters, I've tried sending the input to every window and panel I could find, but to no avail. This post made me curious to think that maybe this WM_CHAR might help? Or maybe you have an idea of what's going on and how I could fix it? Thanks!

    ReplyDelete
    Replies
    1. Hi Scott, I've never tried video game automation, but it might behave like cmd.exe that can handle WM_CHAR message. In pywinauto 0.6.0 it's possible to call method send_chars() that is very similar to type_keys(), but it works for cmd.exe.

      send_chars() implementation is not finished though so consider it as experimental way. It may work for simple keys but may not work for combinations like Ctrl+A. Hope it will help in your case.

      Is this game emulator available for download?

      Delete
  2. Hi Vasily,
    Thanks for your reply. I tried using send_chars(), no luck. I failed to mention that previously I also tried using SendKeys directly from the SendKeysCtypes library, that didn't work either. It must be something about how the emulator handles keyboard input and the mapping.

    The emulator ("console") is available for download, however getting a game (ROM) is a little stickier, since downloaded ROMs are illegal. I bought my game and dumped the ROM so that I could use it with an emulator. The emulator can be obtained at:
    https://sourceforge.net/projects/vbam/

    ReplyDelete
    Replies
    1. Hmm... If the emulator behaves like console, I can suggest one more way. Try using standard class subprocess.Popen with sending keystrokes to stdin.

      Delete