title: How to call a function whenever a key is pressed in python
body: <p>I have a program that is running a loop.
  Whenever I press, for example, the key "ESC" on keyboard, it should call out a function which prints "You pressed the key ESC" and maybe executes some commands too.</p>
  
  <p>I tried this:</p>
  
  <pre><code>from msvcrt import getch
  
  while True:
      key = ord(getch())
      if key == 27: #ESC
          print("You pressed ESC")
      elif key == 13: #Enter
          print("You pressed key ENTER")
          functionThatTerminatesTheLoop()
  </code></pre>
  
  <p>After all my tries, msvcrt doesnt seem to work in python 3.3 or for whatever other reason.
  Basically, how do I make my program react to any keypress at any point of the time while the program is running?</p>
  
  <p>EDIT: Also, I found this:</p>
  
  <pre><code>import sys
  
  while True:
      char = sys.stdin.read(1)
      print ("You pressed: "+char)
      char = sys.stdin.read(1)
  </code></pre>
  
  <p>But it requires enter to be entered into the command console for the input to be reistered, but I have my loop running in a tkinter, so I still need a way for it to do something immediately after the keypress is detected.</p>