summaryrefslogtreecommitdiff
path: root/keyboardpython/__init__.py
blob: 6bfc0f588590e9ad2f326c39777477f091964d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import key, Queue

def getkey(fd='default',buffersize=6):
    ''' Read keypress and return Key object.

        - fd='default' will try and get this stdin.
        - buffersize is passed to os.read, 6 is 
        enough for alt+F12 for example.
    '''
    import sys,os,termios
    if fd == 'default': fd = sys.stdin.fileno()
    tty = termios.tcgetattr(fd)
    # the next 4 lines are voodoo from stackoverflow, without them
    # os.read waits for enter key to return
    tty[3] = tty[3] & ~termios.ICANON & ~termios.ECHO
    tty[6][termios.VMIN] = 1
    tty[6][termios.VTIME] = 0
    termios.tcsetattr(fd, termios.TCSANOW, tty)
    c = os.read(fd,buffersize)
    return key.parse_code(c)