0

I need to give input as F3 (0x72) to an exe file in windows by using python. I use Popen command to issue commands in it. Can anyone guide me?

You can find the details of the program here: How do I get all of the output from my .exe using subprocess and Popen?

Community
  • 1
  • 1
Ajith
  • 59
  • 1
  • 1
  • 7
  • What do you mean by "input as F3 (0x72)"? If you want to just send it the same `stdin` as if someone typed F3 into the console, that's easy, but it's unlikely to do any good. If you want to send keypress events at its main window as if someone had typed F3 while it was in the foreground, you need to get a handle to the window, generate the `WM_KEY*` messages, and post them to the window. (There are probably libraries to help with that, but it's never going to be as simple as scripting a shell filter program.) – abarnert Sep 28 '12 at 09:22
  • I need to type in F3 and F10 to the exe file while it is running. I am planning to automate the key press of F3 and F10 – Ajith Sep 28 '12 at 10:43
  • If this program has a GUI, you might consider using [sikuli](http://sikuli.org/) instead. – Burhan Khalid Sep 28 '12 at 18:49

1 Answers1

0

The answer depends on whether the "exe file" is a Win32 GUI application, or a console application.

If it's a GUI app, you don't just need to "type in F3 and F10 to the exe file while it's running", you need to type them to the application's main window while it's active. If it's a console app, you need to know which of the various different ways it's reading the keyboard and do the appropriate thing—or you can just type them to the console window while it's active.

If you can just assume that the appropriate window is active, look at SendKeys, a low-level module that's designed to do nothing but sending keystroke combinations to whatever window is in the foreground:

SendKeys('{F3}')

If you need more control—e.g., if you need to work even if the app isn't in the foreground—there are a half dozen or so "window automation libraries" for Python. None of them are quite as nice as something like AutoIt, but if you need to use Python, they're not bad. For example, with pywinauto, the pseudocode for what you want to do is something like this:

app = application.find("foo.exe")
wnd = app.TheAppWindow # assuming the app's main window has a title similar to "The App Window"
wnd.TypeKeys('{F3}')

Of course you don't get much benefit out of a library like this unless you need to interact with the GUI—open the menu item named "Edit | Find", type "foo" into the first edit box in the "Find" window, click the button labeled "Next", get the selected text in the main window, etc.

Finally, you can always drop down to the low level and make Win32API calls via PyWin32. The pseudocode is:

hwnd = win32gui.FindWindowEx(0, 0, 'TheAppClass', 'The App Window')
win32gui.PostMessage(hwnd, WM_KEYDOWN, VK_F3, 0)
win32gui.PostMessage(hwnd, WM_KEYUP, VK_F3, 0)

Not really much more complicated, except that now you need the exact window name and the window class. (To find that, use any of the free "window explorer" tools out there; I think Visual C++ Express still comes with one.)

The advantage here is that you know exactly what's going on, which can make problems easier to debug—and which also means you can take advantage of the detailed documentation at MSDN (although PyWin32 itself comes with some pretty decent docs, so you often don't have to).

abarnert
  • 354,177
  • 51
  • 601
  • 671