-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.py
executable file
·42 lines (33 loc) · 966 Bytes
/
keys.py
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
33
34
35
36
37
38
39
40
41
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Hackweek 13, learning Python
#
# just to find out how to cheaply get key events without blocking the terminal ...
import atexit
from select import select
import sys
import termios
import time
# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to normal terminal
def set_normal_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
# switch to unbuffered terminal
def set_curses_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)
def kbhit():
dr, dw, de = select([sys.stdin], [], [], 0)
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
return dr != []
atexit.register(set_normal_term)
set_curses_term()
for i in range(10):
print 'Did you pressed any key? ...', kbhit()
time.sleep(1)