Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 66 additions & 23 deletions readchar/key.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2014, 2015 Miguel Ángel García (@magmax9).
# Based on previous work on gist getch()-like unbuffered character
# reading from stdin on both Windows and Unix (Python recipe),
# started by Danny Yoo. Licensed under the MIT license.

# common
LF = '\x0d'
CR = '\x0a'
ENTER = '\x0d'
BACKSPACE = '\x7f'
SUPR = ''
SPACE = '\x20'
ESC = '\x1b'
TAB = '\x09'

# CTRL
CTRL_A = '\x01'
Expand All @@ -16,6 +21,8 @@
CTRL_F = '\x06'
CTRL_Z = '\x1a'

LEFTBRACKET = '\x5b'

# ALT
ALT_A = '\x1b\x61'

Expand Down Expand Up @@ -50,30 +57,66 @@
END = '\x1b\x5b\x46'

INSERT = '\x1b\x5b\x32\x7e'
SUPR = '\x1b\x5b\x33\x7e'

DELETE = '\x1b\x5b\x33\x7e'

ESCAPE_SEQUENCES = (
ESC,
ESC + '\x5b',
ESC + '\x5b' + '\x31',
ESC + '\x5b' + '\x32',
ESC + '\x5b' + '\x33',
ESC + '\x5b' + '\x35',
ESC + '\x5b' + '\x36',
ESC + '\x5b' + '\x31' + '\x35',
ESC + '\x5b' + '\x31' + '\x36',
ESC + '\x5b' + '\x31' + '\x37',
ESC + '\x5b' + '\x31' + '\x38',
ESC + '\x5b' + '\x31' + '\x39',
ESC + '\x5b' + '\x32' + '\x30',
ESC + '\x5b' + '\x32' + '\x31',
ESC + '\x5b' + '\x32' + '\x32',
ESC + '\x5b' + '\x32' + '\x33',
ESC + '\x5b' + '\x32' + '\x34',
ESC + LEFTBRACKET,
ESC + LEFTBRACKET + '\x31',
ESC + LEFTBRACKET + '\x32',
ESC + LEFTBRACKET + '\x33',
ESC + LEFTBRACKET + '\x35',
ESC + LEFTBRACKET + '\x36',
ESC + LEFTBRACKET + '\x31' + '\x35',
ESC + LEFTBRACKET + '\x31' + '\x36',
ESC + LEFTBRACKET + '\x31' + '\x37',
ESC + LEFTBRACKET + '\x31' + '\x38',
ESC + LEFTBRACKET + '\x31' + '\x39',
ESC + LEFTBRACKET + '\x32' + '\x30',
ESC + LEFTBRACKET + '\x32' + '\x31',
ESC + LEFTBRACKET + '\x32' + '\x32',
ESC + LEFTBRACKET + '\x32' + '\x33',
ESC + LEFTBRACKET + '\x32' + '\x34',
ESC + '\x4f',
ESC + ESC,
ESC + ESC + '\x5b',
ESC + ESC + '\x5b' + '\x32',
ESC + ESC + '\x5b' + '\x33',
ESC + ESC + LEFTBRACKET,
ESC + ESC + LEFTBRACKET + '\x32',
ESC + ESC + LEFTBRACKET + '\x33',
)

linux_keys = {
# Single keys:
CR: 'cr',
ENTER: 'enter',
BACKSPACE: 'backspace',
ESC: 'escape',
HOME: "home",
UP: 'up',
TAB: 'tab',
RIGHT: 'right',
DOWN: 'down',
LEFT: 'left',
PAGE_UP: 'page_up',
PAGE_DOWN: 'page_down',
END: "end",
F1: 'f1',
F2: 'f2',
F3: 'f3',
F4: 'f4',
F5: 'f5',
F6: 'f6',
F7: 'f7',
F8: 'f8',
F9: 'f9',
F10: 'f10',
DELETE: 'delete',
CTRL_A: 'ctrl_a',
CTRL_B: 'ctrl_b',
CTRL_C: 'ctrl_c',
CTRL_D: 'ctrl_d',
CTRL_E: 'ctrl_e',
CTRL_F: 'ctrl_f',
CTRL_Z: 'ctrl_z',
INSERT: 'insert',
CTRL_ALT_A: 'ctrl_alt_a'
}