Getting an input from arrow keys.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Getting an input from arrow keys.

 
0
  #1
Oct 8th, 2009
Okay, I'm trying to make a python game (not with pygame), I want it to use the arrow keys. But the thing is, I don't know how to get it to respond to the arrow keys. So here's what I want to do.

If the user presses the up arrow key set the variable 'direction' to up, if the press the down arrow key set direction to 'down' etc. Would this be possible? If so, how? The only thing I know is that you need to use KeyboardInterupt or something like that.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
0
  #2
Oct 8th, 2009
If you a using a GUI/event-driven framework, then yes, it's possible. If you are using console python, then no.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #3
Oct 9th, 2009
I'm perfectly fine with using tkinter. But I don't even know how to do that .
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
Oct 9th, 2009
Here is an example of a Tkinter key logger ...
  1. # KeyLogger_tk2.py
  2. # show a character key when pressed without using Enter key
  3. # hide the Tkinter GUI window, only console shows
  4.  
  5. try:
  6. # Python2
  7. import Tkinter as tk
  8. except ImportError:
  9. # Python3
  10. import tkinter as tk
  11.  
  12. def key(event):
  13. """shows key or tk code for the key"""
  14. if event.keysym == 'Escape':
  15. root.destroy()
  16. if event.char == event.keysym:
  17. # normal number and letter characters
  18. print( 'Normal Key %r' % event.char )
  19. elif len(event.char) == 1:
  20. # charcters like []/.,><#$ also Return and ctrl/key
  21. print( 'Punctuation Key %r (%r)' % (event.keysym, event.char) )
  22. else:
  23. # f1 to f12, shift keys, caps lock, Home, End, Delete ...
  24. print( 'Special Key %r' % event.keysym )
  25.  
  26.  
  27. root = tk.Tk()
  28. print( "Press a key (Escape key to exit):" )
  29. root.bind_all('<Key>', key)
  30. # don't show the tk window
  31. root.withdraw()
  32. root.mainloop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #5
Oct 9th, 2009
Thank you! you gave me much more then I asked for, as a result I'll be able to use much more then just the arrow keys.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC