-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
41 lines (35 loc) · 1.18 KB
/
util.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
def windowsMessageBox(title, text, style=0):
import ctypes
MessageBox = ctypes.windll.user32.MessageBoxA
return MessageBox(None, text, title, 0x40 | style)
def getkey(key=None):
key=kbhit()
if key==True:
return getch()
else:
return key
return None
try:
from msvcrt import kbhit, getch
except ImportError:
import termios, fcntl, sys, os
def kbhit():
while True:
try:
c = sys.stdin.read(1)
return c
except IOError:
return False
def set_term():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
return oldterm, oldflags
def restore_term(oldterm, oldflags):
fd = sys.stdin.fileno()
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)