I have to know what key is pressed, but not need the code of the Character, i want to know when someone press the 'A' key even if the key obtained is 'a' or 'A', and so with all other keys.
I can't use PyGame or any other library (including Tkinter). Only Python Standard Library. And this have to be done in a terminal, not a graphical interface.
NOT NEED THE CHARACTER CODE. I NEED TO KNOW THE KEY CODE.
Ex:
ord('a') != ord('A') # 97 != 65
someFunction('a') == someFunction('A') # a_code == A_code
If you need to work in windows only you should try msvcrt.
Take a look at pynput module in Python. It also has a nice tutorial using which you can easily create keyboard listeners for your code.
The official example for listeners is:
Hope this helps.
this function will return the code for the uppercase character:
and this is for the lowercase character code:
this way is far easier, and you won't need to import external libraries.
You will need to choose one of the two methods to be the 'someFunction' you described in your question. Here's an example:
OUTPUT:
See tty standard module. It allows switching from default line-oriented (cooked) mode into char-oriented (cbreak) mode with tty.setcbreak(sys.stdin). Reading single char from sys.stdin will result into next pressed keyboard key (if it generates code):
Note: solution is Unix (including Linux) only.
Edit: On Windows try msvcrt.getche()/getwche(). /me has nowhere to try...
Edit 2: Utilize win32 low-level console API via ctypes.windll (see example at SO) with
ReadConsoleInput
function. You should filter out keypresses -e.EventType==KEY_EVENT
and look fore.Event.KeyEvent.wVirtualKeyCode
value. Example of application (not in Python, just to get an idea) can be found at http://www.benryves.com/tutorials/?t=winconsole&c=4.Depending on what you are trying to accomplish, perhaps using a library such as pygame would do what you want. Pygame contains more advanced keypress handling than is normally available with Python's standard libraries.
You probably will have to use Tkinter, which is the 'standard' Python gui, and has been included with python for many years.
A command-line solution is probably not available, because of the way data passes into and out of command-line processes. GUI programs (of some flavor or another) all recieve user-input through a (possibly library wrapped) event stream. Each event will be a record of the event's details. For keystroke events, the record will may contain any of a keycode, modifier key bitfield, or text character in some encoding. Which fields, and how they are named depends on the event library you are calling.
Command-line programs recieve user input through character-streams. There is no way to catch lower-level data. As myroslav explained in his post, tty's can be in cooked or uncooked mode, the only difference being that in cooked mode the terminal will process (some) control characters for you, like delete and enter so that the process receives lines of input, instead of 1 character at a time.
Processing anything lower than that requires (OS dependent) system calls or opening character devices in /dev. Python's standard library provides no standard facility for this.