mirror of
https://github.com/google/mozc-devices.git
synced 2025-11-08 16:53:28 +03:00
Merge pull request #20 from google/enter_and_arrow
Support Enter, Arrows, Backspace, and Delete keys in console mode
This commit is contained in:
@@ -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]))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user