forked from deepfakes/faceswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypress.py
92 lines (77 loc) · 2.96 KB
/
keypress.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""
Source: http://home.wlu.edu/~levys/software/kbhit.py
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X). Doesn't work
with IDLE.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import os
# Windows
if os.name == "nt":
import msvcrt # pylint: disable=import-error
# Posix (Linux, OS X)
else:
import sys
import termios
import atexit
from select import select
class KBHit:
""" Creates a KBHit object that you can call to do various keyboard things. """
def __init__(self, is_gui=False):
self.is_gui = is_gui
if os.name == "nt" or self.is_gui:
pass
else:
# Save the terminal settings
self.file_desc = sys.stdin.fileno()
self.new_term = termios.tcgetattr(self.file_desc)
self.old_term = termios.tcgetattr(self.file_desc)
# New terminal setting unbuffered
self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
termios.tcsetattr(self.file_desc, termios.TCSAFLUSH, self.new_term)
# Support normal-terminal reset at exit
atexit.register(self.set_normal_term)
def set_normal_term(self):
""" Resets to normal terminal. On Windows this is a no-op. """
if os.name == "nt" or self.is_gui:
pass
else:
termios.tcsetattr(self.file_desc, termios.TCSAFLUSH, self.old_term)
@staticmethod
def getch():
""" Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow(). """
if os.name == "nt":
return msvcrt.getch().decode("utf-8")
return sys.stdin.read(1)
@staticmethod
def getarrow():
""" Returns an arrow-key code after kbhit() has been called. Codes are
0 : up
1 : right
2 : down
3 : left
Should not be called in the same program as getch(). """
if os.name == "nt":
msvcrt.getch() # skip 0xE0
char = msvcrt.getch()
vals = [72, 77, 80, 75]
else:
char = sys.stdin.read(3)[2]
vals = [65, 67, 66, 68]
return vals.index(ord(char.decode("utf-8")))
@staticmethod
def kbhit():
""" Returns True if keyboard character was hit, False otherwise. """
if os.name == "nt":
return msvcrt.kbhit()
d_r, _, _ = select([sys.stdin], [], [], 0)
return d_r != []