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 trial

Python Python Collections (2016, retired 2019) Dungeon Game Win or Lose

How 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
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The 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

Thanks Chris!

Ronald Williams
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ronald Williams
Java Web Development Techdegree Graduate 25,021 Points

You 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