Esempio n. 1
0
    def _event_text_symbol(self, ev):
        text = None
        symbol = xlib.KeySym()
        buffer = create_string_buffer(128)

        # Look up raw keysym before XIM filters it (default for keypress and
        # keyrelease)
        count = xlib.XLookupString(ev.xkey, buffer, len(buffer) - 1, byref(symbol), None)

        # Give XIM a shot
        filtered = xlib.XFilterEvent(ev, ev.xany.window)

        if ev.type == xlib.KeyPress and not filtered:
            status = c_int()
            if _have_utf8:
                encoding = "utf8"
                count = xlib.Xutf8LookupString(
                    self._x_ic, ev.xkey, buffer, len(buffer) - 1, byref(symbol), byref(status)
                )
                if status.value == xlib.XBufferOverflow:
                    raise NotImplementedError("TODO: XIM buffer resize")

            else:
                encoding = "ascii"
                count = xlib.XLookupString(ev.xkey, buffer, len(buffer) - 1, byref(symbol), None)
                if count:
                    status.value = xlib.XLookupBoth

            if status.value & (xlib.XLookupChars | xlib.XLookupBoth):
                text = buffer.value[:count].decode(encoding)

            # Don't treat Unicode command codepoints as text, except Return.
            if text and unicodedata.category(text) == "Cc" and text != "\r":
                text = None

        symbol = symbol.value

        # If the event is a XIM filtered event, the keysym will be virtual
        # (e.g., aacute instead of A after a dead key).  Drop it, we don't
        # want these kind of key events.
        if ev.xkey.keycode == 0 and not filtered:
            symbol = None

        # fos.lib.pyglet.self.key keysymbols are identical to X11 keysymbols, no
        # need to map the keysymbol.  For keysyms outside the fos.lib.pyglet set, map
        # raw key code to a user key.
        if symbol and symbol not in key._key_names and ev.xkey.keycode:
            # Issue 353: Symbol is uppercase when shift key held down.
            symbol = ord(unichr(symbol).lower())

            # If still not recognised, use the keycode
            if symbol not in key._key_names:
                symbol = key.user_key(ev.xkey.keycode)

        if filtered:
            # The event was filtered, text must be ignored, but the symbol is
            # still good.
            return None, symbol

        return text, symbol
Esempio n. 2
0
    def _event_key(self, msg, wParam, lParam):
        repeat = False
        if lParam & (1 << 30):
            if msg not in (WM_KEYUP, WM_SYSKEYUP):
                repeat = True
            ev = 'on_key_release'
        else:
            ev = 'on_key_press'

        symbol = keymap.get(wParam, None)
        if symbol is None:
            ch = _user32.MapVirtualKeyW(wParam, MAPVK_VK_TO_CHAR)
            symbol = chmap.get(ch)

        if symbol is None:
            symbol = key.user_key(wParam)
        elif symbol == key.LCTRL and lParam & (1 << 24):
            symbol = key.RCTRL
        elif symbol == key.LALT and lParam & (1 << 24):
            symbol = key.RALT
        elif symbol == key.LSHIFT:
            pass  # TODO: some magic with getstate to find out if it's the
            # right or left shift key.

        modifiers = self._get_modifiers(lParam)

        if not repeat:
            self.dispatch_event(ev, symbol, modifiers)

        ctrl = modifiers & key.MOD_CTRL != 0
        if (symbol, ctrl) in _motion_map and msg not in (WM_KEYUP,
                                                         WM_SYSKEYUP):
            motion = _motion_map[symbol, ctrl]
            if modifiers & key.MOD_SHIFT:
                self.dispatch_event('on_text_motion_select', motion)
            else:
                self.dispatch_event('on_text_motion', motion)

        # Send on to DefWindowProc if not exclusive.
        if self._exclusive_keyboard:
            return 0
        else:
            return None
Esempio n. 3
0
    def _event_key(self, msg, wParam, lParam):
        repeat = False
        if lParam & (1 << 30):
            if msg not in (WM_KEYUP, WM_SYSKEYUP):
                repeat = True
            ev = 'on_key_release'
        else:
            ev = 'on_key_press'

        symbol = keymap.get(wParam, None)
        if symbol is None:
            ch = _user32.MapVirtualKeyW(wParam, MAPVK_VK_TO_CHAR)
            symbol = chmap.get(ch)

        if symbol is None:
            symbol = key.user_key(wParam)
        elif symbol == key.LCTRL and lParam & (1 << 24):
            symbol = key.RCTRL
        elif symbol == key.LALT and lParam & (1 << 24):
            symbol = key.RALT
        elif symbol == key.LSHIFT:
            pass # TODO: some magic with getstate to find out if it's the
                 # right or left shift key. 

        modifiers = self._get_modifiers(lParam)
        
        if not repeat:
            self.dispatch_event(ev, symbol, modifiers)

        ctrl = modifiers & key.MOD_CTRL != 0
        if (symbol, ctrl) in _motion_map and msg not in (WM_KEYUP, WM_SYSKEYUP):
            motion = _motion_map[symbol, ctrl]
            if modifiers & key.MOD_SHIFT:
                self.dispatch_event('on_text_motion_select', motion)
            else:
                self.dispatch_event('on_text_motion', motion)

        # Send on to DefWindowProc if not exclusive.
        if self._exclusive_keyboard:
            return 0
        else:
            return None
Esempio n. 4
0
 def _get_symbol_and_modifiers(ev):
     # The unicode char help processing virtual keycodes (see issue 405)
     wchar = c_wchar()
     carbon.GetEventParameter(
         ev, kEventParamKeyUnicodes, typeUnicodeText, c_void_p(), sizeof(wchar), c_void_p(), byref(wchar)
     )
     wchar = str((wchar.value)).upper()
     # If the unicode char is within charmap keys (ascii value), then we use
     # the correspinding symbol.
     if wchar in charmap.keys():
         symbol = charmap[wchar]
     else:
         sym = c_uint32()
         carbon.GetEventParameter(
             ev, kEventParamKeyCode, typeUInt32, c_void_p(), sizeof(sym), c_void_p(), byref(sym)
         )
         symbol = keymap.get(sym.value, None)
         if symbol is None:
             symbol = key.user_key(sym.value)
     modifiers = c_uint32()
     carbon.GetEventParameter(
         ev, kEventParamKeyModifiers, typeUInt32, c_void_p(), sizeof(modifiers), c_void_p(), byref(modifiers)
     )
     return (symbol, CarbonWindow._map_modifiers(modifiers.value))
Esempio n. 5
0
 def _get_symbol_and_modifiers(ev):
     # The unicode char help processing virtual keycodes (see issue 405)
     wchar = c_wchar()
     carbon.GetEventParameter(ev, kEventParamKeyUnicodes, typeUnicodeText,
                              c_void_p(), sizeof(wchar), c_void_p(),
                              byref(wchar))
     wchar = str((wchar.value)).upper()
     # If the unicode char is within charmap keys (ascii value), then we use
     # the correspinding symbol.
     if wchar in charmap.keys():
         symbol = charmap[wchar]
     else:
         sym = c_uint32()
         carbon.GetEventParameter(ev, kEventParamKeyCode, typeUInt32,
                                  c_void_p(), sizeof(sym), c_void_p(),
                                  byref(sym))
         symbol = keymap.get(sym.value, None)
         if symbol is None:
             symbol = key.user_key(sym.value)
     modifiers = c_uint32()
     carbon.GetEventParameter(ev, kEventParamKeyModifiers, typeUInt32,
                              c_void_p(), sizeof(modifiers), c_void_p(),
                              byref(modifiers))
     return (symbol, CarbonWindow._map_modifiers(modifiers.value))
Esempio n. 6
0
    def _event_text_symbol(self, ev):
        text = None
        symbol = xlib.KeySym()
        buffer = create_string_buffer(128)

        # Look up raw keysym before XIM filters it (default for keypress and
        # keyrelease)
        count = xlib.XLookupString(ev.xkey, buffer,
                                   len(buffer) - 1, byref(symbol), None)

        # Give XIM a shot
        filtered = xlib.XFilterEvent(ev, ev.xany.window)

        if ev.type == xlib.KeyPress and not filtered:
            status = c_int()
            if _have_utf8:
                encoding = 'utf8'
                count = xlib.Xutf8LookupString(self._x_ic, ev.xkey, buffer,
                                               len(buffer) - 1, byref(symbol),
                                               byref(status))
                if status.value == xlib.XBufferOverflow:
                    raise NotImplementedError('TODO: XIM buffer resize')

            else:
                encoding = 'ascii'
                count = xlib.XLookupString(ev.xkey, buffer,
                                           len(buffer) - 1, byref(symbol),
                                           None)
                if count:
                    status.value = xlib.XLookupBoth

            if status.value & (xlib.XLookupChars | xlib.XLookupBoth):
                text = buffer.value[:count].decode(encoding)

            # Don't treat Unicode command codepoints as text, except Return.
            if text and unicodedata.category(text) == 'Cc' and text != '\r':
                text = None

        symbol = symbol.value

        # If the event is a XIM filtered event, the keysym will be virtual
        # (e.g., aacute instead of A after a dead key).  Drop it, we don't
        # want these kind of key events.
        if ev.xkey.keycode == 0 and not filtered:
            symbol = None

        # fos.lib.pyglet.self.key keysymbols are identical to X11 keysymbols, no
        # need to map the keysymbol.  For keysyms outside the fos.lib.pyglet set, map
        # raw key code to a user key.
        if symbol and symbol not in key._key_names and ev.xkey.keycode:
            # Issue 353: Symbol is uppercase when shift key held down.
            symbol = ord(unichr(symbol).lower())

            # If still not recognised, use the keycode
            if symbol not in key._key_names:
                symbol = key.user_key(ev.xkey.keycode)

        if filtered:
            # The event was filtered, text must be ignored, but the symbol is
            # still good.
            return None, symbol

        return text, symbol