944,181 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4523
  • Python RSS
Oct 8th, 2009
0

Getting an input from arrow keys.

Expand Post »
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.
Similar Threads
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 8th, 2009
0
Re: Getting an input from arrow keys.
If you a using a GUI/event-driven framework, then yes, it's possible. If you are using console python, then no.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 9th, 2009
0
Re: Getting an input from arrow keys.
I'm perfectly fine with using tkinter. But I don't even know how to do that .
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 9th, 2009
0
Re: Getting an input from arrow keys.
Here is an example of a Tkinter key logger ...
python Syntax (Toggle Plain Text)
  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()
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 9th, 2009
0
Re: Getting an input from arrow keys.
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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Dec 27th, 2009
0
Re: Getting an input from arrow keys.
The Tkinter code has been working great for me- However, I was wondering if there was a way to pass additional variables into the key function. I also haven't been able to get it to work without showing the tk window.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jorlittl is offline Offline
1 posts
since Dec 2009
Dec 27th, 2009
0
Re: Getting an input from arrow keys.
If you're using windows, I don't suggest you use that any more, use this:

Python Syntax (Toggle Plain Text)
  1. from msvcrt import getch
  2. from threading import Thread
  3.  
  4. ### BACKGROUND CODE
  5.  
  6. def KeyCheck():
  7. global Break_KeyCheck
  8. Break_KeyCheck = False
  9.  
  10. while Break_KeyCheck:
  11. base = getch()
  12. if base == '\xe0':
  13. sub = getch()
  14.  
  15. if sub == 'H':
  16. key = 'UP_KEY'
  17.  
  18. elif sub == 'M':
  19. key = 'RIGHT_KEY'
  20.  
  21. elif sub == 'P':
  22. key = 'DOWN_KEY'
  23.  
  24. elif sub == 'K':
  25. key = 'LEFT_KEY'
  26.  
  27. Thread(target = KeyCheck).start()
  28.  
  29. ### BACKGROUND CODE


It will constantly set the variable 'key', to whatever arrow key you press. If you wish to stop the background code at any time just do 'Break_KeyCheck = True' if you want to start it again do 'Break_KeyCheck = False'. I hope that helps. This also solves the issue, if you click on the screen it stops working. This is more simpler, so you should be able to use this to solve your issue.
Last edited by AutoPython; Dec 27th, 2009 at 3:38 pm.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 3rd, 2011
0
Re: Getting an input from arrow keys.
The code does not work on my Mac. If I comment out the withdrawing of the root window then it works fine as long as I make the root window focused. Tested for Python 3.2.2 and Python 2.7.2.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Szepi is offline Offline
3 posts
since Nov 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with recursive function
Next Thread in Python Forum Timeline: Tkinter for console getkey, Pause/Restart/Quit





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC