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.  
  3. import Tkinter as tk
  4.  
  5. def keypress ( event ) :
  6. if event. keysym == 'Escape' :
  7. root. destroy ( )
  8. x = event. char
  9. if x == "w" :
  10. print "blaw blaw blaw"
  11. elif x == "a" :
  12. print "blaha blaha blaha"
  13. elif x == "s" :
  14. print "blash blash blash"
  15. elif x == "d" :
  16. print "blad blad blad"
  17. else :
  18. print x
  19.  
  20.  
  21. root = tk. Tk ( )
  22. print "Press a key (Escape key to exit):"
  23. root. bind_all ( '<Key >' , keypress )
  24. # don't show the tk window
  25. root. withdraw ( )
  26. root. mainloop ( )

你可能感兴趣的:(Python Get Key press)