aboutsummaryrefslogtreecommitdiffstats
path: root/getch.py
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-03-16 23:54:34 +0200
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-03-17 23:02:34 +0200
commit8665af9d1eb0a5d343f9f1f6fe2c4c185c1aced3 (patch)
tree580cee069b48f6a4cc62188964a662b7493e4dd2 /getch.py
parent6c3aad9839d41a13d7674626b195c2c5b152c333 (diff)
Initial commit
Diffstat (limited to 'getch.py')
-rw-r--r--getch.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/getch.py b/getch.py
new file mode 100644
index 0000000..be6203f
--- /dev/null
+++ b/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()