Skip to content
Closed
Show file tree
Hide file tree
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
102 changes: 102 additions & 0 deletions readchar/keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#These following keys are ones that have 2 meanings.
#The first key command is the one that is not in the dict below:
#ctrl_i is tab
#ctrl_[ is escape
#ctrl_2 is ctrl_c
#ctrl_h is backspace
#ctrl_j is ctrl_enter
#ctrl_m is enter

windows_special_keys = {
#Single keys:
'H': 'up',
'M': 'right',
'P': 'down',
'K': 'left',
';': 'f1',
'<': 'f2',
'=': 'f3',
'>': 'f4',
'?': 'f5',
'@': 'f6',
'A': 'f7',
'B': 'f8',
'C': 'f9',
'D': 'f10',
'S': 'delete',
#key combos
#shifted_keys
'T': 'shift_f1',
'U': 'shift_f2',
'V': 'shift_f3',
'W': 'shift_f4',
'X': 'shift_f5',
'Y': 'shift_f6',
'Z': 'shift_f7',
'[': 'shift_f8',
'\\': 'shift_f9',
']': 'shift_f10',
#ctrl_keys
'^': 'ctrl_f1',
'_': 'ctrl_f2',
'`': 'ctrl_f3',
'a': 'ctrl_f4',
'b': 'ctrl_f5',
'c': 'ctrl_f6',
'd': 'ctrl_f7',
'e': 'ctrl_f8',
'f': 'ctrl_f9',
'g': 'ctrl_f10',
'\x8d': 'ctrl_up',
'\x91': 'ctrl_down',
's': 'ctrl_left',
't': 'ctrl_right',
}

windows_keys = {
#Single keys:
'\\x1b': 'escape',
' ': 'space',
'\\x85': 'f11',
'\\x86': 'f12',
'\\x08': 'backspace',
'\\r': 'enter',
'\\t': 'tab',
#key combos
#shifted_keys
'\\x87': 'shift_f11',
'\\x88': 'shift_f12',
#ctrl_keys
'\\x89': 'ctrl_f11',
'\\x8a': 'ctrl_f12',
'\\x93': 'ctrl_delete',
'\\n': 'ctrl_enter',
'\\x94': 'ctrl_tab',
'\\x7f': 'ctrl_backspace',
'\\x1c': 'ctrl_\\',
'\\x1d': 'ctrl_]',
#ctrl letters
'\\x01': 'ctrl_a',
'\\x02': 'ctrl_b',
'\\x03': 'ctrl_c',
'\\x04': 'ctrl_d',
'\\x05': 'ctrl_e',
'\\x06': 'ctrl_f',
'\\x07': 'ctrl_g',
'\\x0b': 'ctrl_k',
'\\x0c': 'ctrl_l',
'\\x0e': 'ctrl_n',
'\\x0f': 'ctrl_o',
'\\x10': 'ctrl_p',
'\\x11': 'ctrl_q',
'\\x12': 'ctrl_r',
'\\x13': 'ctrl_s',
'\\x14': 'ctrl_t',
'\\x15': 'ctrl_u',
'\\x16': 'ctrl_v',
'\\x17': 'ctrl_w',
'\\x18': 'ctrl_x',
'\\x19': 'ctrl_y',
'\\x1a': 'ctrl_z',
'\\\\': '\\',
}
5 changes: 2 additions & 3 deletions readchar/readchar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
from . import key


if sys.platform.startswith('linux'):
from .readchar_linux import readchar
elif sys.platform == 'darwin':
Expand All @@ -17,9 +16,9 @@
raise NotImplemented('The platform %s is not supported yet' % sys.platform)


def readkey(getchar_fn=None):
def readkey(getchar_fn=None, blocking=True):
getchar = getchar_fn or readchar
buffer = getchar(True)
buffer = getchar(blocking)

while True:
if buffer not in key.ESCAPE_SEQUENCES:
Expand Down
32 changes: 24 additions & 8 deletions readchar/readchar_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
# http://code.activestate.com/recipes/134892/#c9
# Thanks to Stephen Chappell
import msvcrt
from keys import windows_keys, windows_special_keys

def get_char():
"""Get a single character on Windows."""
ch = msvcrt.getch()
if ch in '\x00\xe0':
ch = msvcrt.getch()
ch = windows_special_keys.get(ch, ch)
ch = repr(ch)[1:-1]
if ch.startswith('\\') or ch == ' ':
ch = windows_keys.get(ch, ch)
return ch

def readchar(blocking=False):
"Get a single character on Windows."
"""gets a character or combo on windows and returns a string. If blocking is True then it will catch ctrl+c and not have them end the program. It will also wait for a key to be pressed before continuing on with the loop."""
if blocking:
return get_char()
else:
if msvcrt.kbhit():
return get_char()

while msvcrt.kbhit():
msvcrt.getch()
ch = msvcrt.getch()
while ch in '\x00\xe0':
msvcrt.getch()
ch = msvcrt.getch()
return ch.decode()
if __name__ == '__main__':
while True:
c = readchar()
if c:
print(c)
if c == 'e':
break