summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-06-01 14:40:30 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-06-01 14:58:44 +0100
commita6310dce41fd777629ddbea5e9977d6f505459ea (patch)
tree7748cd0566bf2d88fc7bdb95319d1c1677304e99
parentf8311306f6499a992c21a231361afc62132cbef3 (diff)
downloadkeyboard-python-a6310dce41fd777629ddbea5e9977d6f505459ea.tar.bz2
Add echo, noecho, cbreak and nocbreak
To make more like curses for familiarity
-rw-r--r--keyboardpython/__init__.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
index c50708a..f97283a 100644
--- a/keyboardpython/__init__.py
+++ b/keyboardpython/__init__.py
@@ -1,22 +1,43 @@
from key import *
+def echo(on=True):
+ import sys,termios
+ fd = sys.stdin.fileno()
+ t = termios.tcgetattr(fd)
+ if on: t[3] = t[3] & termios.ECHO
+ else: t[3] = t[3] & ~termios.ECHO
+ termios.tcsetattr(fd, termios.TCSANOW, t)
+
+def noecho():
+ echo(False)
+
+def cbreak(on=True):
+ import sys,termios
+ fd = sys.stdin.fileno()
+ t = termios.tcgetattr(fd)
+ if on: t[3] = t[3] & ~termios.ICANON
+ else: t[3] = t[3] & termios.ICANON
+ termios.tcsetattr(fd, termios.TCSANOW, t)
+
+def nocbreak():
+ cbreak(False)
+
def set_to_read_key(fd='default'):
''' Set tty so that os.read gets one keypress at a time '''
import sys,termios
# VMIN min bytes read before returning from os.read()
- VMIN = 0
+ VMIN = 0 # 0 for pure timed read
# VTIME tenths of a second elapses between bytes read
VTIME = 1
if fd == 'default': fd = sys.stdin.fileno()
global before_t
before_t = termios.tcgetattr(fd)
t = termios.tcgetattr(fd)
- # turn echo and needing to press enter off
- t[3] = t[3] & ~termios.ICANON & ~termios.ECHO
+ noecho()
+ cbreak()
cc = t[-1]
cc[termios.VMIN] = VMIN
cc[termios.VTIME] = VTIME
- termios.tcsetattr(fd, termios.TCSANOW, t)
def reset_tty(fd='default'):
''' set tty to how it was before set_to_read_key '''