Python|Python Get Key press

refer from:http://www.daniweb.com/forums/thread115282.html#

python Syntax ( Toggle Plain Text )


  1. # respond to a key without the need to press enter
  2. import Tkinter as tk
  3. def keypress ( event ) :
  4. if event. keysym == 'Escape' :
  5. root. destroy ( )
  6. x = event. char
  7. if x == "w" :
  8. print "blaw blaw blaw"
  9. elif x == "a" :
  10. print "blaha blaha blaha"
  11. elif x == "s" :
  12. print "blash blash blash"
  13. elif x == "d" :
  14. print "blad blad blad"
  15. else :
  16. print x
  17. root = tk. Tk ( )
  18. print "Press a key (Escape key to exit):"
  19. root. bind_all ( '' , keypress )
  20. # don't show the tk window
  21. root. withdraw ( )
  22. root. mainloop ( )

    推荐阅读