key_test.py 649 Bytes
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
import tty
import termios
import select
import time


def readchar():
    r, w, e = select.select([sys.stdin], [], [])
    if sys.stdin in r:
        ch = sys.stdin.read(1)
    return ch


fd = sys.stdin.fileno()
oldtty = termios.tcgetattr(fd)
newtty = termios.tcgetattr(fd)
try:
    termios.tcsetattr(fd, termios.TCSANOW, newtty)
    tty.setraw(fd)
    tty.setcbreak(fd)
    while True:
        print('Wait')
        time.sleep(0.1)
        key = readchar()
        print('%d' % ord(key))
        if key == 'w':
            print('w')
        if key == 'q':
            break
finally:
    termios.tcsetattr(fd, termios.TCSADRAIN, oldtty)