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_keys(): """ Handle keyboard events. """ # avoid blocking on ttys if there's no input if plat.stdin_is_tty and not kbhit(): return s = sys.stdin.readline().decode('utf-8') if s == '': backend.close_input() for u in s: c = u.encode('utf-8') # replace LF -> CR if needed if c == '\n' and lf_to_cr: c = '\r' try: backend.insert_chars(unicodepage.from_utf8(c)) except KeyError: backend.insert_chars(c)
def get_line(self): """ Read a line from the file. """ if self.cache != None: line, eof = self.cache self.cache = None else: line, eof, self.last = read_program_line(self.source, self.last, self.cr) if not self.universal: return line, eof # keep adding lines until one starts with a number while not eof: line2, eof2, self.last = read_program_line(self.source, self.last, self.cr) i = 0 while i < len(line2) and line2[i] in (' ', '\0'): i += 1 # if not starting with a number after whitespace, concatenate if i == len(line2) or line2[i] < '0' or line2[i] > '9': line = line[:-1] + '\n' + line2 eof = eof2 else: break if not eof: self.cache = line2, eof2 if line and line[-1] == '\n': line = line[:-1] + '\r' if self.utf8: # decode and encode back as an easy way to split up in single-character multibyte sequences try: utf8line = line.decode('utf-8') rline = '' for c in utf8line: try: rline += unicodepage.from_utf8(c.encode('utf-8')) except KeyError: # pass unknown sequences untranslated. this includes \r. rline += c.encode('utf-8') line = rline except UnicodeDecodeError: # not valid UTF8, pass through raw. pass return line, eof
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)