Ejemplo n.º 1
0
 def on_key_event(self,
                  key_event: KeyEvent,
                  in_bracketed_paste: bool = False) -> None:
     if key_event.text:
         if self.state is State.command:
             self.line_edit.on_text(key_event.text, in_bracketed_paste)
             self.draw_status_line()
             return
         if self.state is State.message:
             self.state = State.diffed
             self.draw_status_line()
             return
     else:
         if self.state is State.message:
             if key_event.type is not EventType.RELEASE:
                 self.state = State.diffed
                 self.draw_status_line()
             return
         if self.state is State.command:
             if self.line_edit.on_key(key_event):
                 if not self.line_edit.current_input:
                     self.state = State.diffed
                 self.draw_status_line()
                 return
             if key_event.matches('enter'):
                 self.state = State.diffed
                 self.do_search()
                 self.line_edit.clear()
                 self.draw_screen()
                 return
             if key_event.matches('esc'):
                 self.state = State.diffed
                 self.draw_status_line()
                 return
         if self.state.value >= State.diffed.value and self.current_search is not None and key_event.matches(
                 'esc'):
             self.current_search = None
             self.draw_screen()
             return
         if key_event.type is EventType.RELEASE:
             return
     action = self.shortcut_action(key_event)
     if action is not None:
         return self.perform_action(action)
Ejemplo n.º 2
0
 def on_key(self, key_event: KeyEvent) -> None:
     if key_event.type is EventType.RELEASE:
         return
     if key_event.matches('esc'):
         self.quit_loop(0)
         return
     if key_event.key in ('w', 'n', 't', 's') and key_event.mods == CTRL:
         self.do_window_resize(is_decrease=key_event.key in 'ns',
                               is_horizontal=key_event.key in 'wn',
                               multiplier=2)
Ejemplo n.º 3
0
 def on_key(self, key_event: KeyEvent) -> None:
     if key_event.matches('backspace'):
         self.current_input = self.current_input[:-1]
         self.current_text = None
         self.draw_screen()
     elif (key_event.matches('enter') or key_event.matches('space')) and self.current_input:
         try:
             idx = decode_hint(self.current_input, self.alphabet)
             self.chosen.append(self.index_map[idx])
             self.ignore_mark_indices.add(idx)
         except Exception:
             self.current_input = ''
             self.current_text = None
             self.draw_screen()
         else:
             if self.multiple:
                 self.reset()
                 self.draw_screen()
             else:
                 self.quit_loop(0)
     elif key_event.matches('esc'):
         self.quit_loop(0 if self.multiple else 1)
Ejemplo n.º 4
0
def encode_key_mapping(window, key_mapping):
    mods, key = parse_shortcut(key_mapping)
    event = KeyEvent(
        mods=mods,
        key=key,
        shift=bool(mods & 1),
        alt=bool(mods & 2),
        ctrl=bool(mods & 4),
        super=bool(mods & 8),
        hyper=bool(mods & 16),
        meta=bool(mods & 32),
    ).as_window_system_event()

    return window.encoded_key(event)
Ejemplo n.º 5
0
def encode_key_mapping(key_mapping, window):
    mods, key = parse_shortcut(key_mapping)
    event = KeyEvent(
        mods=mods,
        key=key,
        shift=bool(mods & SHIFT),
        alt=bool(mods & ALT),
        ctrl=bool(mods & CTRL),
        super=bool(mods & SUPER),
        hyper=bool(mods & HYPER),
        meta=bool(mods & META),
    ).as_window_system_event()

    return window.encoded_key(event)
Ejemplo n.º 6
0
def encode_key_mapping(key_mapping):
    mods, key = parse_shortcut(key_mapping)
    event = KeyEvent(
        mods=mods,
        key=key,
        shift=bool(mods & 1),
        alt=bool(mods & 2),
        ctrl=bool(mods & 4),
        super=bool(mods & 8),
        hyper=bool(mods & 16),
        meta=bool(mods & 32),
    ).as_window_system_event()

    return encode_key_for_tty(event.key, event.shifted_key,
                              event.alternate_key, event.mods, event.action)
Ejemplo n.º 7
0
 def on_key(self, key_event: KeyEvent) -> bool:
     if key_event.type is EventType.RELEASE:
         return False
     if key_event.matches('home'):
         return self.home()
     if key_event.matches('end'):
         return self.end()
     if key_event.matches('backspace'):
         self.backspace()
         return True
     if key_event.matches('delete'):
         self.delete()
         return True
     if key_event.matches('left'):
         self.left()
         return True
     if key_event.matches('right'):
         self.right()
         return True
     return False
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    def on_key(self, key_event: KeyEvent) -> None:
        if self.mode is HEX and key_event.type is not EventType.RELEASE and not key_event.has_mods:
            try:
                val = int(self.line_edit.current_input, 16)
            except Exception:
                pass
            else:
                if key_event.matches('tab'):
                    self.line_edit.current_input = hex(val + 0x10)[2:]
                    self.refresh()
                    return
                if key_event.matches('up'):
                    self.line_edit.current_input = hex(val + 1)[2:]
                    self.refresh()
                    return
                if key_event.matches('down'):
                    self.line_edit.current_input = hex(val - 1)[2:]
                    self.refresh()
                    return
        if self.mode is NAME:
            if key_event.matches('shift+tab'):
                self.table.move_current(cols=-1)
                self.refresh()
                return
            if key_event.matches('tab'):
                self.table.move_current(cols=1)
                self.refresh()
                return
            if key_event.matches('left'):
                self.table.move_current(cols=-1)
                self.refresh()
                return
            if key_event.matches('right'):
                self.table.move_current(cols=1)
                self.refresh()
                return
            if key_event.matches('up'):
                self.table.move_current(rows=-1)
                self.refresh()
                return
            if key_event.matches('down'):
                self.table.move_current(rows=1)
                self.refresh()
                return

        if self.line_edit.on_key(key_event):
            self.refresh()
            return
        if key_event.matches('enter'):
            self.quit_loop(0)
            return
        if key_event.matches('esc'):
            self.quit_loop(1)
            return
        if key_event.matches_without_mods('f1') or key_event.matches('ctrl+1'):
            self.switch_mode(HEX)
            return
        if key_event.matches_without_mods('f2') or key_event.matches('ctrl+2'):
            self.switch_mode(NAME)
            return
        if key_event.matches_without_mods('f3') or key_event.matches('ctrl+3'):
            self.switch_mode(EMOTICONS)
            return
        if key_event.matches_without_mods('f4') or key_event.matches('ctrl+4'):
            self.switch_mode(FAVORITES)
            return
        if key_event.matches_without_mods('f12') and self.mode is FAVORITES:
            self.edit_favorites()
            return
        if key_event.matches('ctrl+shift+tab'):
            self.next_mode(-1)
            return
        for key in ('tab', '[', ']'):
            if key_event.matches(f'ctrl+{key}'):
                self.next_mode(-1 if key == '[' else 1)
                return