Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKeith Ostertag
16,619 PointsHow to implement using arrow keys for the game? Maybe a command recall?
How would I go about implementing arrow keys for this game instead of having to type out the movement words (RIGHT, LEFT, UP, DOWN)? Scan codes? Ascii codes? Unicodes? I have no clue. Would it be OS independent?
I guess another consideration is that not everyone has the same keyboard... so is there a difference between using the separate arrow keys on a 101-key keyboard rather than using the numlock pad arrows?
Somewhat related.... how to implement a command recall? In the case I am not using the arrow keys for movement, it would be nice to be able to use the up arrow key for command recall so I wouldn't need to retype the move command words each time...
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThe issue with using arrow keys with a regular python script is that the script is running in a command window or shell window. These shells "see" the arrow keys first and react thus "consuming" the key stroke before it can be passed to the python code. This is based on the keycodes for the arrows. The number pad arrows produce the same keycodes as the actual arrowkeys (when not in numlock mode).
The command recall is built into the command shell, the PowerShell and the linux bash shell and the like. To implement your own you need to be able to capture arrow keys, so back to square one.
The best approach would to be to a graphical front end to the program using Tkinter or pygame. See this StackOverflow answer for more hints.
There is also the linux based termios module
Ronald Williams
Java Web Development Techdegree Graduate 25,021 PointsYou could instead use the keys W A S D to represent arrow keys. This is done in many popular games such as World of Warcraft. Where W = UP, A = LEFT, S = DOWN, and D = RIGHT
Chris Freeman
Treehouse Moderator 68,441 PointsThis has the draw back that the shell is still capturing the keystrokes and does not pass them on to the program until the return key is hit.
Ronald Williams
Java Web Development Techdegree Graduate 25,021 PointsYes, but faster than typing out the word.
Keith Ostertag
16,619 PointsKeith Ostertag
16,619 PointsThanks Chris!