-I know that the title can be a bit confusing, therefore, to be able to establish my doubt, I leave this code...
windows...
import msvcrt
while True:
print(msvcrt.getch())
Linux
import getch
while True:
print(getch.getch())
-If you don't have the module, it doesn't matter, basically what the code does is print what the user enters on the screen (without the need for the user to press enter). When I discovered this, a sea of possibilities opened up for me (I'm still a novice), but to date, I have not been able to know if the user presses an arrow, based on this premise, the question would be: how to symbolize the arrows to be able to do something like this...
while True:
entrada = getch.getch()
if entrada == flecha_derecha:
print('Presionaste la flecha derecha')
elif entrada == flecha_izquierda:
print('Presionaste la flecha izquierda')
...
-I know this could be emulated if I just interpreted other keys as arrows (in fact, I made a pretty simple module to print 'interactive' menus, if you want to see it: https://github.com/PySanti/modulos-de -python , I would really appreciate an opinion on what could be improved ), however, it doesn't seem so comfortable to me, and the doubt is eating my soul
In this answer I do not only focus on the arrows issue, but on all possible keys. I did NOT take into account key combinations like
shift+c
. Remember that I am on Windows and I have a qwerty keyboard.I made a function that serves to get both the arrows and the FX keys and other special keys. Next I will show the function with its parts explained and a table with each result that came out on my computer.
Windows
Linux (not tested)
If you don't understand the dictionary of dictionaries, you can see what each key returns by using the following little program alongside my function:
For arrows, the function returns