title: How to send simulated keyboard strokes to the active window using SendKeys
body: <p>I am new to Python and am trying to send a combination of key strokes to an application I open under Windows 7. While my script will work flawlessly when I invoke Notepad.exe for testing purposes, it does not with the actual application I am trying to use. </p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>import win32com.client
  import time
  import SendKeys
  import os
  from ctypes import *
  
  shell = win32com.client.Dispatch("WScript.Shell")
  
  os.startfile("C:\...exe")
  
  time.sleep( 5 )
  
  shell.SendKeys('%{F4}')     # 'Alt+F4' to close the application again.
  </code></pre>
  
  <p>For some reason, the application does not close in reaction to the script. When I hit 'Alt + F4' on my keyboard, it closes as expected. Any ideas on what might be going on here?</p>
  
  <p>Any help is welcome! Please bear in mind that I am new to Python ;-)</p>
  
  <p>PS: I have already verified that the application runs in the active window by including this code snippet:</p>
  
  <pre><code>import win32ui
  wnd = win32ui.GetForegroundWindow()
  print wnd.GetWindowText()
  </code></pre>