Merge pull request #20 from google/enter_and_arrow

Support Enter, Arrows, Backspace, and Delete keys in console mode
This commit is contained in:
Shuhei Iitsuka
2018-09-11 15:24:13 +09:00
committed by GitHub
2 changed files with 37 additions and 2 deletions

View File

@@ -42,6 +42,7 @@ class Bluetooth():
# See http://ww1.microchip.com/downloads/en/DeviceDoc/bluetooth_cr_UG-v1.0r.pdf
# for detail.
UART_CODES = {
'KEY_DELETE': 4,
'KEY_RIGHT': 7,
'KEY_BACKSPACE': 8,
'KEY_ENTER': 10,
@@ -54,4 +55,7 @@ class Bluetooth():
if cmd not in self.UART_CODES:
print('Unknown Command: {}'.format(cmd))
return
if self._dummy:
print('bluetooth: command({})'.format(cmd))
return
self.send(struct.pack('b', self.UART_CODES[cmd]))

View File

@@ -73,6 +73,33 @@ class KeyboardRecorderFromConsole(KeyboardRecorder):
KeyboardRecorder.__init__(self, verbose)
self.log('Input from console')
def _read_ansi_escape(self):
seq = sys.stdin.read(1)
if seq == '[':
# CSI code
# ['\e', '[', Rep (optional), Code]
code = sys.stdin.read(1)
rep = 1
if code.isdigit():
rep = int(code)
code = sys.stdin.read(1)
if code == 'A':
return (None, 'KEY_UP')
if code == 'B':
return (None, 'KEY_DOWN')
if code == 'C':
return (None, 'KEY_RIGHT')
if code == 'D':
return (None, 'KEY_LEFT')
if code == '~':
if rep == 3:
return (None, 'KEY_DELETE')
self.log('it was unknown code: ' +
'rep={}, code={}'.format(rep, code))
else:
self.log('unknown seq: {}'.format(seq))
return (None, None)
def record(self):
"""
Returns a tuple of |data| and |command|.
@@ -93,8 +120,12 @@ class KeyboardRecorderFromConsole(KeyboardRecorder):
key = None
finally:
now = datetime.datetime.now()
if key == '\n':
return (None, None)
if key == '\x1b':
return self._read_ansi_escape();
elif key == '\n':
return (None, 'KEY_ENTER')
elif key == '\b' or key == '\x7f':
return (None, 'KEY_BACKSPACE')
elif key:
if not recording:
recording = True