summaryrefslogtreecommitdiff
path: root/Scripts/getch.py
diff options
context:
space:
mode:
authorJulian <Julianteule@gmail.com>2017-02-01 10:36:42 +0100
committerJulian <Julianteule@gmail.com>2017-02-01 10:36:42 +0100
commit2eb28948556f464980cf4296c09d044c21bc84de (patch)
treea71c8976591986341020b0bc678057b584240047 /Scripts/getch.py
parent076f4801a51216d1d5f4aec9ccfbcf77382fea87 (diff)
cool
Diffstat (limited to 'Scripts/getch.py')
-rwxr-xr-xScripts/getch.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Scripts/getch.py b/Scripts/getch.py
new file mode 100755
index 0000000..be6203f
--- /dev/null
+++ b/Scripts/getch.py
@@ -0,0 +1,38 @@
+class _Getch:
+ """Gets a single character from standard input. Does not echo to the
+screen."""
+ def __init__(self):
+ try:
+ self.impl = _GetchWindows()
+ except ImportError:
+ self.impl = _GetchUnix()
+
+ def __call__(self): return self.impl()
+
+
+class _GetchUnix:
+ def __init__(self):
+ import tty, sys
+
+ def __call__(self):
+ import sys, tty, termios
+ fd = sys.stdin.fileno()
+ old_settings = termios.tcgetattr(fd)
+ try:
+ tty.setraw(sys.stdin.fileno())
+ ch = sys.stdin.read(1)
+ finally:
+ termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
+ return ch
+
+
+class _GetchWindows:
+ def __init__(self):
+ import msvcrt
+
+ def __call__(self):
+ import msvcrt
+ return msvcrt.getch()
+
+
+getch = _Getch()