title: How to detect key press when the console window has lost focus
body: <p>My application sends a mouse click to an area of the screen outside my console window using <code>win32api.mouse_event</code>.  This causes my window to lose focus so I can't detect key presses using <code>msvcrt.kbhit.</code></p>
  
  <p>My question is, how to implement something like this with the console window not being visible/active?</p>
  
  <p>EDIT:</p>
  
  <p>Here is example code. This is just a basic console version. If the console disappears, set it so that it stays on top. Once it clicks outside of the console (100,100), pressing the Esc key will not break out of loop. How would one implement a break for a problem such as this? </p>
  
  <pre><code>import msvcrt, win32api, win32con
  
  pixelx = 100
  pixely = 100
  
  win32api.SetCursorPos((pixelx,pixely))
  win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,pixelx,pixely,0,0)
  win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,pixelx,pixely,0,0)
  
  while 1:
      print 'Testing..'
      # body of the loop ...
      if msvcrt.kbhit():
          if ord(msvcrt.getch()) == 27:
              break
  </code></pre>