def on_key_event(self, key_event: KeyEvent, in_bracketed_paste: bool = False) -> None: etype = { PRESS: 'PRESS', REPEAT: 'REPEAT', RELEASE: 'RELEASE' }[key_event.type] mods = format_mods(key_event.mods) if mods: mods += '+' key = f'{mods}{key_event.key} ' self.cmd.colored(key, 'green') self.cmd.colored(etype + ' ', 'yellow') self.cmd.styled(key_event.text, italic=True) self.print() rep = 'CSI ' + encode_key_event(key_event)[2:] rep = rep.replace(';', ' ; ').replace(':', ' : ')[:-1] + ' ' + rep[-1] self.cmd.styled(rep, fg='magenta') if (key_event.shifted_key or key_event.alternate_key): self.print() if key_event.shifted_key: self.cmd.colored('Shifted key: ', 'gray') self.print(key_event.shifted_key + ' ', end='') if key_event.alternate_key: self.cmd.colored('Alternate key: ', 'gray') self.print(key_event.alternate_key + ' ', end='') self.print() self.print()
def on_key(self, key_event: KeyEventType) -> None: if self.line_edit.on_key(key_event): self.commit_line() if key_event.type is not RELEASE and not key_event.mods: if key_event.key is K['ENTER']: self.write_broadcast_text('\r') self.print('') self.line_edit.clear() self.write(SAVE_CURSOR) return ek = encode_key_event(key_event) self.write_broadcast_data('kitty-key:' + ek)
def on_key(self, key_event: KeyEventType) -> None: if self.line_edit.on_key(key_event): self.commit_line() if key_event.matches('enter'): self.write_broadcast_text('\r') self.print('') self.line_edit.clear() self.write(SAVE_CURSOR) return ek = encode_key_event(key_event) ek = standard_b64encode(ek.encode('utf-8')).decode('ascii') self.write_broadcast_data('kitty-key:' + ek)
def on_key(self, key_event): etype = { PRESS: 'PRESS', REPEAT: 'REPEAT', RELEASE: 'RELEASE' }[key_event.type] mods = [] for m, name in { SHIFT: 'Shift', ALT: 'Alt', CTRL: 'Ctrl', SUPER: 'Super'}.items(): if key_event.mods & m: mods.append(name) mods = '+'.join(mods) if mods: mods += '+' self.print('Key {}: {}{} [{}]'.format(etype, mods, key_event.key, encode_key_event(key_event)))
def on_key_event(self, key_event: KeyEvent, in_bracketed_paste: bool = False) -> None: etype = { PRESS: 'PRESS', REPEAT: 'REPEAT', RELEASE: 'RELEASE' }[key_event.type] lmods = [] for m, name in { SHIFT: 'Shift', ALT: 'Alt', CTRL: 'Ctrl', SUPER: 'Super', HYPER: 'Hyper', META: 'Meta', NUM_LOCK: 'NumLock', CAPS_LOCK: 'CapsLock', }.items(): if key_event.mods & m: lmods.append(name) mods = '+'.join(lmods) if mods: mods += '+' key = f'{mods}{key_event.key} ' self.cmd.colored(key, 'green') self.cmd.colored(etype + ' ', 'yellow') self.cmd.styled(key_event.text, italic=True) self.print() rep = 'CSI ' + encode_key_event(key_event)[2:] rep = rep.replace(';', ' ; ').replace(':', ' : ')[:-1] + ' ' + rep[-1] self.cmd.styled(rep, fg='magenta') if (key_event.shifted_key or key_event.alternate_key): self.print() if key_event.shifted_key: self.cmd.colored('Shifted key: ', 'gray') self.print(key_event.shifted_key + ' ', end='') if key_event.alternate_key: self.cmd.colored('Alternate key: ', 'gray') self.print(key_event.alternate_key + ' ', end='') self.print() self.print()
def test_encode_key_event(self): enc = defines.encode_key_for_tty ae = self.assertEqual shift, alt, ctrl, super = defines.GLFW_MOD_SHIFT, defines.GLFW_MOD_ALT, defines.GLFW_MOD_CONTROL, defines.GLFW_MOD_SUPER # noqa press, repeat, release = defines.GLFW_PRESS, defines.GLFW_REPEAT, defines.GLFW_RELEASE # noqa def csi(mods=0, num=1, action=1, shifted_key=0, alternate_key=0, text=None, trailer='u'): ans = '\033[' if isinstance(num, str): num = ord(num) if num != 1 or mods or shifted_key or alternate_key or text: ans += f'{num}' if shifted_key or alternate_key: if isinstance(shifted_key, str): shifted_key = ord(shifted_key) ans += ':' + (f'{shifted_key}' if shifted_key else '') if alternate_key: if isinstance(alternate_key, str): alternate_key = ord(alternate_key) ans += f':{alternate_key}' if mods or action > 1 or text: m = 0 if mods & shift: m |= 1 if mods & alt: m |= 2 if mods & ctrl: m |= 4 if mods & super: m |= 8 if action > 1 or m: ans += f';{m+1}' if action > 1: ans += f':{action}' elif text: ans += ';' if text: ans += ';' + ':'.join(map(str, map(ord, text))) return ans + trailer def mods_test(key, plain=None, shift=None, ctrl=None, alt=None, calt=None, cshift=None, ashift=None, csi_num=None, trailer='u'): c = partial(csi, num=csi_num or key, trailer=trailer) e = partial(enc, key=key) def a(a, b): ae(a, b, f"{a.encode('ascii')} != {b.encode('ascii')}") a(e(), plain or c()) a(e(mods=defines.GLFW_MOD_SHIFT), shift or c(defines.GLFW_MOD_SHIFT)) a(e(mods=defines.GLFW_MOD_CONTROL), ctrl or c(defines.GLFW_MOD_CONTROL)) a(e(mods=defines.GLFW_MOD_ALT | defines.GLFW_MOD_CONTROL), calt or c(defines.GLFW_MOD_ALT | defines.GLFW_MOD_CONTROL)) a(e(mods=defines.GLFW_MOD_SHIFT | defines.GLFW_MOD_CONTROL), cshift or c(defines.GLFW_MOD_CONTROL | defines.GLFW_MOD_SHIFT)) a(e(mods=defines.GLFW_MOD_SHIFT | defines.GLFW_MOD_ALT), ashift or c(defines.GLFW_MOD_ALT | defines.GLFW_MOD_SHIFT)) mods_test(defines.GLFW_FKEY_ENTER, '\x0d', alt='\033\x0d', csi_num=ord('\r')) mods_test(defines.GLFW_FKEY_KP_ENTER, '\x0d', alt='\033\x0d', csi_num=ord('\r')) mods_test(defines.GLFW_FKEY_ESCAPE, '\x1b', alt='\033\033', csi_num=27) mods_test(defines.GLFW_FKEY_BACKSPACE, '\x7f', alt='\033\x7f', ctrl='\x08', csi_num=127) mods_test(defines.GLFW_FKEY_TAB, '\t', alt='\033\t', shift='\x1b[Z', csi_num=ord('\t')) mods_test(defines.GLFW_FKEY_INSERT, csi_num=2, trailer='~') mods_test(defines.GLFW_FKEY_KP_INSERT, csi_num=2, trailer='~') mods_test(defines.GLFW_FKEY_DELETE, csi_num=3, trailer='~') mods_test(defines.GLFW_FKEY_KP_DELETE, csi_num=3, trailer='~') mods_test(defines.GLFW_FKEY_PAGE_UP, csi_num=5, trailer='~') mods_test(defines.GLFW_FKEY_KP_PAGE_UP, csi_num=5, trailer='~') mods_test(defines.GLFW_FKEY_KP_PAGE_DOWN, csi_num=6, trailer='~') mods_test(defines.GLFW_FKEY_HOME, csi_num=1, trailer='H') mods_test(defines.GLFW_FKEY_KP_HOME, csi_num=1, trailer='H') mods_test(defines.GLFW_FKEY_END, csi_num=1, trailer='F') mods_test(defines.GLFW_FKEY_KP_END, csi_num=1, trailer='F') mods_test(defines.GLFW_FKEY_F1, csi_num=1, trailer='P') mods_test(defines.GLFW_FKEY_F2, csi_num=1, trailer='Q') mods_test(defines.GLFW_FKEY_F3, csi_num=1, trailer='R') mods_test(defines.GLFW_FKEY_F4, csi_num=1, trailer='S') mods_test(defines.GLFW_FKEY_F5, csi_num=15, trailer='~') mods_test(defines.GLFW_FKEY_F6, csi_num=17, trailer='~') mods_test(defines.GLFW_FKEY_F7, csi_num=18, trailer='~') mods_test(defines.GLFW_FKEY_F8, csi_num=19, trailer='~') mods_test(defines.GLFW_FKEY_F9, csi_num=20, trailer='~') mods_test(defines.GLFW_FKEY_F10, csi_num=21, trailer='~') mods_test(defines.GLFW_FKEY_F11, csi_num=23, trailer='~') mods_test(defines.GLFW_FKEY_F12, csi_num=24, trailer='~') mods_test(defines.GLFW_FKEY_UP, csi_num=1, trailer='A') mods_test(defines.GLFW_FKEY_KP_UP, csi_num=1, trailer='A') mods_test(defines.GLFW_FKEY_DOWN, csi_num=1, trailer='B') mods_test(defines.GLFW_FKEY_KP_DOWN, csi_num=1, trailer='B') mods_test(defines.GLFW_FKEY_RIGHT, csi_num=1, trailer='C') mods_test(defines.GLFW_FKEY_KP_RIGHT, csi_num=1, trailer='C') mods_test(defines.GLFW_FKEY_LEFT, csi_num=1, trailer='D') mods_test(defines.GLFW_FKEY_KP_LEFT, csi_num=1, trailer='D') # legacy key tests {{{ # start legacy letter tests (auto generated by gen-key-constants.py do not edit) ae(enc(ord('`')), '`') ae(enc(ord('`'), mods=shift), '~') ae(enc(ord('`'), mods=alt), "\x1b" + '`') ae(enc(ord('`'), mods=shift | alt), "\x1b" + '~') ae(enc(ord('`'), mods=ctrl), '`') ae(enc(ord('`'), mods=ctrl | alt), "\x1b" + '`') ae(enc(ord('1')), '1') ae(enc(ord('1'), mods=shift), '!') ae(enc(ord('1'), mods=alt), "\x1b" + '1') ae(enc(ord('1'), mods=shift | alt), "\x1b" + '!') ae(enc(ord('1'), mods=ctrl), '1') ae(enc(ord('1'), mods=ctrl | alt), "\x1b" + '1') ae(enc(ord('2')), '2') ae(enc(ord('2'), mods=shift), '@') ae(enc(ord('2'), mods=alt), "\x1b" + '2') ae(enc(ord('2'), mods=shift | alt), "\x1b" + '@') ae(enc(ord('2'), mods=ctrl), '\x00') ae(enc(ord('2'), mods=ctrl | alt), "\x1b" + '\x00') ae(enc(ord('3')), '3') ae(enc(ord('3'), mods=shift), '#') ae(enc(ord('3'), mods=alt), "\x1b" + '3') ae(enc(ord('3'), mods=shift | alt), "\x1b" + '#') ae(enc(ord('3'), mods=ctrl), '\x1b') ae(enc(ord('3'), mods=ctrl | alt), "\x1b" + '\x1b') ae(enc(ord('4')), '4') ae(enc(ord('4'), mods=shift), '$') ae(enc(ord('4'), mods=alt), "\x1b" + '4') ae(enc(ord('4'), mods=shift | alt), "\x1b" + '$') ae(enc(ord('4'), mods=ctrl), '\x1c') ae(enc(ord('4'), mods=ctrl | alt), "\x1b" + '\x1c') ae(enc(ord('5')), '5') ae(enc(ord('5'), mods=shift), '%') ae(enc(ord('5'), mods=alt), "\x1b" + '5') ae(enc(ord('5'), mods=shift | alt), "\x1b" + '%') ae(enc(ord('5'), mods=ctrl), '\x1d') ae(enc(ord('5'), mods=ctrl | alt), "\x1b" + '\x1d') ae(enc(ord('6')), '6') ae(enc(ord('6'), mods=shift), '^') ae(enc(ord('6'), mods=alt), "\x1b" + '6') ae(enc(ord('6'), mods=shift | alt), "\x1b" + '^') ae(enc(ord('6'), mods=ctrl), '\x1e') ae(enc(ord('6'), mods=ctrl | alt), "\x1b" + '\x1e') ae(enc(ord('7')), '7') ae(enc(ord('7'), mods=shift), '&') ae(enc(ord('7'), mods=alt), "\x1b" + '7') ae(enc(ord('7'), mods=shift | alt), "\x1b" + '&') ae(enc(ord('7'), mods=ctrl), '\x1f') ae(enc(ord('7'), mods=ctrl | alt), "\x1b" + '\x1f') ae(enc(ord('8')), '8') ae(enc(ord('8'), mods=shift), '*') ae(enc(ord('8'), mods=alt), "\x1b" + '8') ae(enc(ord('8'), mods=shift | alt), "\x1b" + '*') ae(enc(ord('8'), mods=ctrl), '\x7f') ae(enc(ord('8'), mods=ctrl | alt), "\x1b" + '\x7f') ae(enc(ord('9')), '9') ae(enc(ord('9'), mods=shift), '(') ae(enc(ord('9'), mods=alt), "\x1b" + '9') ae(enc(ord('9'), mods=shift | alt), "\x1b" + '(') ae(enc(ord('9'), mods=ctrl), '9') ae(enc(ord('9'), mods=ctrl | alt), "\x1b" + '9') ae(enc(ord('0')), '0') ae(enc(ord('0'), mods=shift), ')') ae(enc(ord('0'), mods=alt), "\x1b" + '0') ae(enc(ord('0'), mods=shift | alt), "\x1b" + ')') ae(enc(ord('0'), mods=ctrl), '0') ae(enc(ord('0'), mods=ctrl | alt), "\x1b" + '0') ae(enc(ord('-')), '-') ae(enc(ord('-'), mods=shift), '_') ae(enc(ord('-'), mods=alt), "\x1b" + '-') ae(enc(ord('-'), mods=shift | alt), "\x1b" + '_') ae(enc(ord('-'), mods=ctrl), '-') ae(enc(ord('-'), mods=ctrl | alt), "\x1b" + '-') ae(enc(ord('=')), '=') ae(enc(ord('='), mods=shift), '+') ae(enc(ord('='), mods=alt), "\x1b" + '=') ae(enc(ord('='), mods=shift | alt), "\x1b" + '+') ae(enc(ord('='), mods=ctrl), '=') ae(enc(ord('='), mods=ctrl | alt), "\x1b" + '=') ae(enc(ord('[')), '[') ae(enc(ord('['), mods=shift), '{') ae(enc(ord('['), mods=alt), "\x1b" + '[') ae(enc(ord('['), mods=shift | alt), "\x1b" + '{') ae(enc(ord('['), mods=ctrl), '\x1b') ae(enc(ord('['), mods=ctrl | alt), "\x1b" + '\x1b') ae(enc(ord(']')), ']') ae(enc(ord(']'), mods=shift), '}') ae(enc(ord(']'), mods=alt), "\x1b" + ']') ae(enc(ord(']'), mods=shift | alt), "\x1b" + '}') ae(enc(ord(']'), mods=ctrl), '\x1d') ae(enc(ord(']'), mods=ctrl | alt), "\x1b" + '\x1d') ae(enc(ord('\\')), '\\') ae(enc(ord('\\'), mods=shift), '|') ae(enc(ord('\\'), mods=alt), "\x1b" + '\\') ae(enc(ord('\\'), mods=shift | alt), "\x1b" + '|') ae(enc(ord('\\'), mods=ctrl), '\x1c') ae(enc(ord('\\'), mods=ctrl | alt), "\x1b" + '\x1c') ae(enc(ord(';')), ';') ae(enc(ord(';'), mods=shift), ':') ae(enc(ord(';'), mods=alt), "\x1b" + ';') ae(enc(ord(';'), mods=shift | alt), "\x1b" + ':') ae(enc(ord(';'), mods=ctrl), ';') ae(enc(ord(';'), mods=ctrl | alt), "\x1b" + ';') ae(enc(ord("'")), "'") ae(enc(ord("'"), mods=shift), '"') ae(enc(ord("'"), mods=alt), "\x1b" + "'") ae(enc(ord("'"), mods=shift | alt), "\x1b" + '"') ae(enc(ord("'"), mods=ctrl), "'") ae(enc(ord("'"), mods=ctrl | alt), "\x1b" + "'") ae(enc(ord(',')), ',') ae(enc(ord(','), mods=shift), '<') ae(enc(ord(','), mods=alt), "\x1b" + ',') ae(enc(ord(','), mods=shift | alt), "\x1b" + '<') ae(enc(ord(','), mods=ctrl), ',') ae(enc(ord(','), mods=ctrl | alt), "\x1b" + ',') ae(enc(ord('.')), '.') ae(enc(ord('.'), mods=shift), '>') ae(enc(ord('.'), mods=alt), "\x1b" + '.') ae(enc(ord('.'), mods=shift | alt), "\x1b" + '>') ae(enc(ord('.'), mods=ctrl), '.') ae(enc(ord('.'), mods=ctrl | alt), "\x1b" + '.') ae(enc(ord('/')), '/') ae(enc(ord('/'), mods=shift), '?') ae(enc(ord('/'), mods=alt), "\x1b" + '/') ae(enc(ord('/'), mods=shift | alt), "\x1b" + '?') ae(enc(ord('/'), mods=ctrl), '\x1f') ae(enc(ord('/'), mods=ctrl | alt), "\x1b" + '\x1f') ae(enc(ord('a')), 'a') ae(enc(ord('a'), mods=shift), 'A') ae(enc(ord('a'), mods=alt), "\x1b" + 'a') ae(enc(ord('a'), mods=shift | alt), "\x1b" + 'A') ae(enc(ord('a'), mods=ctrl), '\x01') ae(enc(ord('a'), mods=ctrl | alt), "\x1b" + '\x01') ae(enc(ord('b')), 'b') ae(enc(ord('b'), mods=shift), 'B') ae(enc(ord('b'), mods=alt), "\x1b" + 'b') ae(enc(ord('b'), mods=shift | alt), "\x1b" + 'B') ae(enc(ord('b'), mods=ctrl), '\x02') ae(enc(ord('b'), mods=ctrl | alt), "\x1b" + '\x02') ae(enc(ord('c')), 'c') ae(enc(ord('c'), mods=shift), 'C') ae(enc(ord('c'), mods=alt), "\x1b" + 'c') ae(enc(ord('c'), mods=shift | alt), "\x1b" + 'C') ae(enc(ord('c'), mods=ctrl), '\x03') ae(enc(ord('c'), mods=ctrl | alt), "\x1b" + '\x03') ae(enc(ord('d')), 'd') ae(enc(ord('d'), mods=shift), 'D') ae(enc(ord('d'), mods=alt), "\x1b" + 'd') ae(enc(ord('d'), mods=shift | alt), "\x1b" + 'D') ae(enc(ord('d'), mods=ctrl), '\x04') ae(enc(ord('d'), mods=ctrl | alt), "\x1b" + '\x04') ae(enc(ord('e')), 'e') ae(enc(ord('e'), mods=shift), 'E') ae(enc(ord('e'), mods=alt), "\x1b" + 'e') ae(enc(ord('e'), mods=shift | alt), "\x1b" + 'E') ae(enc(ord('e'), mods=ctrl), '\x05') ae(enc(ord('e'), mods=ctrl | alt), "\x1b" + '\x05') ae(enc(ord('f')), 'f') ae(enc(ord('f'), mods=shift), 'F') ae(enc(ord('f'), mods=alt), "\x1b" + 'f') ae(enc(ord('f'), mods=shift | alt), "\x1b" + 'F') ae(enc(ord('f'), mods=ctrl), '\x06') ae(enc(ord('f'), mods=ctrl | alt), "\x1b" + '\x06') ae(enc(ord('g')), 'g') ae(enc(ord('g'), mods=shift), 'G') ae(enc(ord('g'), mods=alt), "\x1b" + 'g') ae(enc(ord('g'), mods=shift | alt), "\x1b" + 'G') ae(enc(ord('g'), mods=ctrl), '\x07') ae(enc(ord('g'), mods=ctrl | alt), "\x1b" + '\x07') ae(enc(ord('h')), 'h') ae(enc(ord('h'), mods=shift), 'H') ae(enc(ord('h'), mods=alt), "\x1b" + 'h') ae(enc(ord('h'), mods=shift | alt), "\x1b" + 'H') ae(enc(ord('h'), mods=ctrl), '\x08') ae(enc(ord('h'), mods=ctrl | alt), "\x1b" + '\x08') ae(enc(ord('i')), 'i') ae(enc(ord('i'), mods=shift), 'I') ae(enc(ord('i'), mods=alt), "\x1b" + 'i') ae(enc(ord('i'), mods=shift | alt), "\x1b" + 'I') ae(enc(ord('i'), mods=ctrl), '\t') ae(enc(ord('i'), mods=ctrl | alt), "\x1b" + '\t') ae(enc(ord('j')), 'j') ae(enc(ord('j'), mods=shift), 'J') ae(enc(ord('j'), mods=alt), "\x1b" + 'j') ae(enc(ord('j'), mods=shift | alt), "\x1b" + 'J') ae(enc(ord('j'), mods=ctrl), '\n') ae(enc(ord('j'), mods=ctrl | alt), "\x1b" + '\n') ae(enc(ord('k')), 'k') ae(enc(ord('k'), mods=shift), 'K') ae(enc(ord('k'), mods=alt), "\x1b" + 'k') ae(enc(ord('k'), mods=shift | alt), "\x1b" + 'K') ae(enc(ord('k'), mods=ctrl), '\x0b') ae(enc(ord('k'), mods=ctrl | alt), "\x1b" + '\x0b') ae(enc(ord('l')), 'l') ae(enc(ord('l'), mods=shift), 'L') ae(enc(ord('l'), mods=alt), "\x1b" + 'l') ae(enc(ord('l'), mods=shift | alt), "\x1b" + 'L') ae(enc(ord('l'), mods=ctrl), '\x0c') ae(enc(ord('l'), mods=ctrl | alt), "\x1b" + '\x0c') ae(enc(ord('m')), 'm') ae(enc(ord('m'), mods=shift), 'M') ae(enc(ord('m'), mods=alt), "\x1b" + 'm') ae(enc(ord('m'), mods=shift | alt), "\x1b" + 'M') ae(enc(ord('m'), mods=ctrl), '\r') ae(enc(ord('m'), mods=ctrl | alt), "\x1b" + '\r') ae(enc(ord('n')), 'n') ae(enc(ord('n'), mods=shift), 'N') ae(enc(ord('n'), mods=alt), "\x1b" + 'n') ae(enc(ord('n'), mods=shift | alt), "\x1b" + 'N') ae(enc(ord('n'), mods=ctrl), '\x0e') ae(enc(ord('n'), mods=ctrl | alt), "\x1b" + '\x0e') ae(enc(ord('o')), 'o') ae(enc(ord('o'), mods=shift), 'O') ae(enc(ord('o'), mods=alt), "\x1b" + 'o') ae(enc(ord('o'), mods=shift | alt), "\x1b" + 'O') ae(enc(ord('o'), mods=ctrl), '\x0f') ae(enc(ord('o'), mods=ctrl | alt), "\x1b" + '\x0f') ae(enc(ord('p')), 'p') ae(enc(ord('p'), mods=shift), 'P') ae(enc(ord('p'), mods=alt), "\x1b" + 'p') ae(enc(ord('p'), mods=shift | alt), "\x1b" + 'P') ae(enc(ord('p'), mods=ctrl), '\x10') ae(enc(ord('p'), mods=ctrl | alt), "\x1b" + '\x10') ae(enc(ord('q')), 'q') ae(enc(ord('q'), mods=shift), 'Q') ae(enc(ord('q'), mods=alt), "\x1b" + 'q') ae(enc(ord('q'), mods=shift | alt), "\x1b" + 'Q') ae(enc(ord('q'), mods=ctrl), '\x11') ae(enc(ord('q'), mods=ctrl | alt), "\x1b" + '\x11') ae(enc(ord('r')), 'r') ae(enc(ord('r'), mods=shift), 'R') ae(enc(ord('r'), mods=alt), "\x1b" + 'r') ae(enc(ord('r'), mods=shift | alt), "\x1b" + 'R') ae(enc(ord('r'), mods=ctrl), '\x12') ae(enc(ord('r'), mods=ctrl | alt), "\x1b" + '\x12') ae(enc(ord('s')), 's') ae(enc(ord('s'), mods=shift), 'S') ae(enc(ord('s'), mods=alt), "\x1b" + 's') ae(enc(ord('s'), mods=shift | alt), "\x1b" + 'S') ae(enc(ord('s'), mods=ctrl), '\x13') ae(enc(ord('s'), mods=ctrl | alt), "\x1b" + '\x13') ae(enc(ord('t')), 't') ae(enc(ord('t'), mods=shift), 'T') ae(enc(ord('t'), mods=alt), "\x1b" + 't') ae(enc(ord('t'), mods=shift | alt), "\x1b" + 'T') ae(enc(ord('t'), mods=ctrl), '\x14') ae(enc(ord('t'), mods=ctrl | alt), "\x1b" + '\x14') ae(enc(ord('u')), 'u') ae(enc(ord('u'), mods=shift), 'U') ae(enc(ord('u'), mods=alt), "\x1b" + 'u') ae(enc(ord('u'), mods=shift | alt), "\x1b" + 'U') ae(enc(ord('u'), mods=ctrl), '\x15') ae(enc(ord('u'), mods=ctrl | alt), "\x1b" + '\x15') ae(enc(ord('v')), 'v') ae(enc(ord('v'), mods=shift), 'V') ae(enc(ord('v'), mods=alt), "\x1b" + 'v') ae(enc(ord('v'), mods=shift | alt), "\x1b" + 'V') ae(enc(ord('v'), mods=ctrl), '\x16') ae(enc(ord('v'), mods=ctrl | alt), "\x1b" + '\x16') ae(enc(ord('w')), 'w') ae(enc(ord('w'), mods=shift), 'W') ae(enc(ord('w'), mods=alt), "\x1b" + 'w') ae(enc(ord('w'), mods=shift | alt), "\x1b" + 'W') ae(enc(ord('w'), mods=ctrl), '\x17') ae(enc(ord('w'), mods=ctrl | alt), "\x1b" + '\x17') ae(enc(ord('x')), 'x') ae(enc(ord('x'), mods=shift), 'X') ae(enc(ord('x'), mods=alt), "\x1b" + 'x') ae(enc(ord('x'), mods=shift | alt), "\x1b" + 'X') ae(enc(ord('x'), mods=ctrl), '\x18') ae(enc(ord('x'), mods=ctrl | alt), "\x1b" + '\x18') ae(enc(ord('y')), 'y') ae(enc(ord('y'), mods=shift), 'Y') ae(enc(ord('y'), mods=alt), "\x1b" + 'y') ae(enc(ord('y'), mods=shift | alt), "\x1b" + 'Y') ae(enc(ord('y'), mods=ctrl), '\x19') ae(enc(ord('y'), mods=ctrl | alt), "\x1b" + '\x19') ae(enc(ord('z')), 'z') ae(enc(ord('z'), mods=shift), 'Z') ae(enc(ord('z'), mods=alt), "\x1b" + 'z') ae(enc(ord('z'), mods=shift | alt), "\x1b" + 'Z') ae(enc(ord('z'), mods=ctrl), '\x1a') ae(enc(ord('z'), mods=ctrl | alt), "\x1b" + '\x1a') # end legacy letter tests # }}} ae(enc(key=ord(' ')), ' ') ae(enc(key=ord(' '), mods=ctrl), '\0') ae(enc(key=ord('i'), mods=ctrl | shift), csi(ctrl | shift, ord('i'))) ae(enc(key=defines.GLFW_FKEY_LEFT_SHIFT), '') q = partial(enc, key=ord('a')) ae(q(), 'a') ae(q(text='a'), 'a') ae(q(action=repeat), 'a') ae(q(action=release), '') # test disambiguate dq = partial(enc, key_encoding_flags=0b1) ae(dq(ord('a')), 'a') ae(dq(defines.GLFW_FKEY_ESCAPE), csi(num=27)) ae(dq(defines.GLFW_FKEY_ENTER), '\r') ae(dq(defines.GLFW_FKEY_ENTER, mods=shift), csi(shift, 13)) ae(dq(defines.GLFW_FKEY_TAB), '\t') ae(dq(defines.GLFW_FKEY_BACKSPACE), '\x7f') ae(dq(defines.GLFW_FKEY_TAB, mods=shift), csi(shift, 9)) for mods in (ctrl, alt, ctrl | shift, alt | shift): ae(dq(ord('a'), mods=mods), csi(mods, ord('a'))) ae(dq(ord(' '), mods=ctrl), csi(ctrl, ord(' '))) for k in (defines.GLFW_FKEY_KP_PAGE_UP, defines.GLFW_FKEY_KP_0): ae(dq(k), csi(num=k)) ae(dq(k, mods=ctrl), csi(ctrl, num=k)) ae(dq(defines.GLFW_FKEY_UP), '\x1b[A') ae(dq(defines.GLFW_FKEY_UP, mods=ctrl), csi(ctrl, 1, trailer='A')) # test event type reporting tq = partial(enc, key_encoding_flags=0b10) ae(tq(ord('a')), 'a') ae(tq(ord('a'), action=defines.GLFW_REPEAT), csi(num='a', action=2)) ae(tq(ord('a'), action=defines.GLFW_RELEASE), csi(num='a', action=3)) ae(tq(ord('a'), action=defines.GLFW_RELEASE, mods=shift), csi(shift, num='a', action=3)) # test alternate key reporting aq = partial(enc, key_encoding_flags=0b100) ae(aq(ord('a')), 'a') ae(aq(ord('a'), shifted_key=ord('A')), 'a') ae(aq(ord('a'), mods=shift, shifted_key=ord('A')), csi(shift, 'a', shifted_key='A')) ae(aq(ord('a'), alternate_key=ord('A')), csi(num='a', alternate_key='A')) ae( aq(ord('a'), mods=shift, shifted_key=ord('A'), alternate_key=ord('b')), csi(shift, 'a', shifted_key='A', alternate_key='b')) # test report all keys kq = partial(enc, key_encoding_flags=0b1000) ae(kq(ord('a')), csi(num='a')) ae(kq(ord('a'), action=defines.GLFW_REPEAT), csi(num='a')) ae(kq(ord('a'), mods=ctrl), csi(ctrl, num='a')) ae(kq(defines.GLFW_FKEY_UP), '\x1b[A') ae(kq(defines.GLFW_FKEY_LEFT_SHIFT), csi(num=defines.GLFW_FKEY_LEFT_SHIFT)) ae(kq(defines.GLFW_FKEY_ENTER), '\x1b[13u') ae(kq(defines.GLFW_FKEY_TAB), '\x1b[9u') ae(kq(defines.GLFW_FKEY_BACKSPACE), '\x1b[127u') # test embed text eq = partial(enc, key_encoding_flags=0b11000) ae(eq(ord('a'), text='a'), csi(num='a', text='a')) ae(eq(ord('a'), mods=shift, text='A'), csi(shift, num='a', text='A')) ae(eq(ord('a'), mods=shift, text='AB'), csi(shift, num='a', text='AB')) # test roundtripping via KeyEvent for mods in range(16): for action in EventType: for key in ('ENTER', 'a', 'TAB', 'F3'): for shifted_key in ('', 'X'): for alternate_key in ('', 'Y'): for text in ('', 'moose'): ev = KeyEvent(type=action, mods=mods, key=key, text=text, shifted_key=shifted_key, alternate_key=alternate_key, shift=bool(mods & 1), alt=bool(mods & 2), ctrl=bool(mods & 4), super=bool(mods & 8)) ec = encode_key_event(ev) q = decode_key_event(ec[2:-1], ec[-1]) self.ae(ev, q)