How to obtain the keycodes in Python
Question: 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
    


Answer: See [tty](http://docs.python.org/library/tty.html) standard module. It allows
switching from default line-oriented (cooked) mode into char-oriented (cbreak)
mode with
[tty.setcbreak(sys.stdin)](http://docs.python.org/library/tty.html#tty.setcbreak).
Reading single char from sys.stdin will result into next pressed keyboard key
(if it generates code):

    
    
    import sys
    import tty
    tty.setcbreak(sys.stdin)
    while True:
        print ord(sys.stdin.read(1))
    

_Note: solution is Unix (including Linux) only._

Edit: On Windows try
[msvcrt.getche()](http://docs.python.org/library/msvcrt.html#msvcrt.getche)/[getwche()](http://docs.python.org/library/msvcrt.html#msvcrt.getwche).
/me has nowhere to try...

* * *

Edit 2: Utilize win32 low-level console API via
[ctypes.windll](http://docs.python.org/library/ctypes.html) (see [example at
SO](http://stackoverflow.com/questions/239020/how-can-i-call-a-dll-from-a-
scripting-language)) with `ReadConsoleInput` function. You should filter out
keypresses - `e.EventType==KEY_EVENT` and look for
`e.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](http://www.benryves.com/tutorials/?t=winconsole&c=4).

