def check_keyboard(): """ Handle keyboard events. """ s = '' i = 0 while True: i = window.getch() if i == -1: break elif i == 0: s += '\0\0' elif i < 256: s += chr(i) else: if i == curses.KEY_BREAK: # this is fickle, on many terminals doesn't work backend.insert_special_key('break') elif i == curses.KEY_RESIZE: sys.stdout.write(ansi.esc_resize_term % (height, width)) sys.stdout.flush() window.resize(height, width) window.clear() redraw() try: # scancode, insert here and now # there shouldn't be a mix of special keys and utf8 in one # uninterrupted string, since the only reason an uninterrupted # string would be longer than 1 char is because it's a single # utf-8 sequence or a pasted utf-8 string, neither of which # can contain special characters. # however, if that does occur, this won't work correctly. backend.key_down(curses_to_scan[i], '', check_full=False) except KeyError: pass # replace utf-8 with codepage # convert into unicode codepoints u = s.decode('utf-8') # then handle these one by one as UTF-8 sequences c = '' for uc in u: c += uc.encode('utf-8') if c == '\x03': # send BREAK for ctrl-C backend.insert_special_key('break') elif c == '\0': # scancode; go add next char continue else: try: backend.insert_chars(unicodepage.from_utf8(c)) except KeyError: backend.insert_chars(c) c = ''
def check_keyboard(): """ Handle keyboard events. """ global pre_buffer # s is one utf-8 sequence or one scancode # or a failed attempt at one of the above u8, sc = get_key() if sc: # if it's an ansi sequence/scan code, insert immediately backend.key_down(sc, '', check_full=False) elif u8: if u8 == '\x03': # ctrl-C backend.insert_special_key('break') if u8 == eof: # ctrl-D (unix) / ctrl-Z (windows) backend.insert_special_key('quit') elif u8 == '\x7f': # backspace backend.insert_chars('\b', check_full=True) else: try: backend.insert_chars(unicodepage.from_utf8(u8)) except KeyError: backend.insert_chars(u8)