| | |
Getting an input from arrow keys.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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.
0
#4 Oct 9th, 2009
Here is an example of a Tkinter key logger ...
python Syntax (Toggle Plain Text)
# KeyLogger_tk2.py # show a character key when pressed without using Enter key # hide the Tkinter GUI window, only console shows try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk def key(event): """shows key or tk code for the key""" if event.keysym == 'Escape': root.destroy() if event.char == event.keysym: # normal number and letter characters print( 'Normal Key %r' % event.char ) elif len(event.char) == 1: # charcters like []/.,><#$ also Return and ctrl/key print( 'Punctuation Key %r (%r)' % (event.keysym, event.char) ) else: # f1 to f12, shift keys, caps lock, Home, End, Delete ... print( 'Special Key %r' % event.keysym ) root = tk.Tk() print( "Press a key (Escape key to exit):" ) root.bind_all('<Key>', key) # don't show the tk window root.withdraw() root.mainloop()
May 'the Google' be with you!
![]() |
Similar Threads
- Using arrow keys to navigate around a form (PHP)
- How do you check if arrow keys are pressed? (C)
- how to detect arrow keys?? (C)
- SQL Plus command line: Arrow keys not giving previous commands back (Oracle)
- Find if user hit arrow keys (C++)
- moving around in an array with arrow keys (C++)
- need help with the use of arrow keys (C)
Other Threads in the Python Forum
- Previous Thread: Need help with tic tac toe
- Next Thread: Image processing using python
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect refresh scrolledtext software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






. 