コード例 #1
0
ファイル: keymap.py プロジェクト: chenzhiwei/deepin-ui
def parse_keyevent_name(keyevent_name):
    '''
    Parse keyevent name.

    @param keyevent_name: Key event name that return by function L{ I{get_keyevent_name} <get_keyevent_name>}.
    @return: Return tuple that contain key value and modifier mask, (keyval, modifier_mask).
    '''
    keys = keyevent_name.split(" + ")
    if len(keys) == 1:
        keyval = int(gdk.keyval_from_name(keys[0]))
        modifier_mask = 0
    else:
        keyval = int(gdk.keyval_from_name(keys[-1]))
        modifier_mask = 0

        for modifier in keys[0:-1]:
            if modifier == "Ctrl":
                modifier_mask = modifier_mask | gdk.CONTROL_MASK
            elif modifier == "Super":
                modifier_mask = modifier_mask | gdk.SUPER_MASK
            elif modifier == "Hyper":
                modifier_mask = modifier_mask | gdk.HYPER_MASK
            elif modifier == "Alt":
                modifier_mask = modifier_mask | gdk.MOD1_MASK
            elif modifier == "Shift":
                modifier_mask = modifier_mask | gdk.SHIFT_MASK

        if gdk.keyval_is_upper(keyval) and len(get_key_name(keyval)) == 1:
            keyval = gdk.keyval_to_lower(keyval)
            modifier_mask = modifier_mask | gdk.SHIFT_MASK

    return (keyval, modifier_mask)
コード例 #2
0
ファイル: keymap.py プロジェクト: netphi/deepin-ui
def parse_keyevent_name(keyevent_name):
    '''Parse keyevent name.'''
    keys = keyevent_name.split(" + ")
    if len(keys) == 1:
        keyval = int(gdk.keyval_from_name(keys[0]))
        modifier_mask = 0
    else:
        keyval = int(gdk.keyval_from_name(keys[-1]))
        modifier_mask = 0
        
        for modifier in keys[0:-1]:
            if modifier == "Ctrl":
                modifier_mask = modifier_mask | gdk.CONTROL_MASK
            elif modifier == "Super":
                modifier_mask = modifier_mask | gdk.SUPER_MASK
            elif modifier == "Hyper":
                modifier_mask = modifier_mask | gdk.HYPER_MASK
            elif modifier == "Alt":
                modifier_mask = modifier_mask | gdk.MOD1_MASK
            elif modifier == "Shift":
                modifier_mask = modifier_mask | gdk.SHIFT_MASK
                
        if gdk.keyval_is_upper(keyval) and len(get_key_name(keyval)) == 1:
            keyval = gdk.keyval_to_lower(keyval)
            modifier_mask = modifier_mask | gdk.SHIFT_MASK
            
    return (keyval, modifier_mask)
コード例 #3
0
ファイル: keyboard.py プロジェクト: TuringMachinegun/mypaint
    def _key_press_cb(self, widget, event):
        """App-wide keypress handler for toplevel windows."""

        if not self.enabled:
            return
        # See gtk sourcecode in gtkmenu.c function gtk_menu_key_press,
        # which uses the same code as below when changing an accelerator.
        keymap = gtk2compat.gdk.keymap_get_default()

        # Instead of using event.keyval, we do it the lowlevel way.
        # Reason: ignoring CAPSLOCK and checking if SHIFT was pressed
        state = event.state & ~gdk.LOCK_MASK
        if gtk2compat.USE_GTK3:
            state = gdk.ModifierType(state)
        res = keymap.translate_keyboard_state(event.hardware_keycode, state,
                                              event.group)
        if not res:
            # PyGTK returns None when gdk_keymap_translate_keyboard_state()
            # returns false.  Not sure if this is a bug or a feature - the only
            # time I have seen this happen is when I put my laptop into sleep
            # mode.
            logger.warning('translate_keyboard_state() returned None. '
                           'Strange key pressed?')
            return

        keyval_offset = 1 if gtk2compat.USE_GTK3 else 0
        keyval = res[keyval_offset]
        consumed_modifiers = res[keyval_offset+3]

        # We want to ignore irrelevant modifiers like ScrollLock.  The stored
        # key binding does not include modifiers that affected its keyval.
        modifiers = (
            event.state
            & gtk.accelerator_get_default_mod_mask()
            & ~consumed_modifiers
        )

        # Except that key bindings are always stored in lowercase.
        keyval_lower = gdk.keyval_to_lower(keyval)
        if keyval_lower != keyval:
            modifiers |= gdk.SHIFT_MASK
        action = self.keymap.get((keyval_lower, modifiers))
        if not action:
            # try hardcoded keys
            action = self.keymap2.get((keyval_lower, modifiers))

        # Don't dispatch if the window is only sensitive to a subset of
        # actions, and the action is not in that set.
        if action is not None and isinstance(action, gtk.Action):
            win_actions = self.window_actions.get(widget, None)
            if win_actions is not None:
                if action.get_name() not in win_actions:
                    return False

        # If the lookup succeeded, activate the corresponding action.
        if action:
            return self.activate_keydown_event(action, event)

        # Otherwise, dispatch the event to the active doc.
        return self._dispatch_fallthru_key_press_event(widget, event)
コード例 #4
0
    def _key_press_cb(self, widget, event):
        """App-wide keypress handler for toplevel windows."""

        if not self.enabled:
            return
        # See gtk sourcecode in gtkmenu.c function gtk_menu_key_press,
        # which uses the same code as below when changing an accelerator.
        keymap = gtk2compat.gdk.keymap_get_default()

        # Instead of using event.keyval, we do it the lowlevel way.
        # Reason: ignoring CAPSLOCK and checking if SHIFT was pressed
        state = event.state & ~gdk.LOCK_MASK
        if gtk2compat.USE_GTK3:
            state = gdk.ModifierType(state)
        res = keymap.translate_keyboard_state(event.hardware_keycode, state,
                                              event.group)
        if not res:
            # PyGTK returns None when gdk_keymap_translate_keyboard_state()
            # returns false.  Not sure if this is a bug or a feature - the only
            # time I have seen this happen is when I put my laptop into sleep
            # mode.
            logger.warning('translate_keyboard_state() returned None. '
                           'Strange key pressed?')
            return

        keyval_offset = 1 if gtk2compat.USE_GTK3 else 0
        keyval = res[keyval_offset]
        consumed_modifiers = res[keyval_offset + 3]

        # We want to ignore irrelevant modifiers like ScrollLock.  The stored
        # key binding does not include modifiers that affected its keyval.
        modifiers = (event.state
                     & gtk.accelerator_get_default_mod_mask()
                     & ~consumed_modifiers)

        # Except that key bindings are always stored in lowercase.
        keyval_lower = gdk.keyval_to_lower(keyval)
        if keyval_lower != keyval:
            modifiers |= gdk.SHIFT_MASK
        action = self.keymap.get((keyval_lower, modifiers))
        if not action:
            # try hardcoded keys
            action = self.keymap2.get((keyval_lower, modifiers))

        # Don't dispatch if the window is only sensitive to a subset of
        # actions, and the action is not in that set.
        if action is not None and isinstance(action, gtk.Action):
            win_actions = self.window_actions.get(widget, None)
            if win_actions is not None:
                if action.get_name() not in win_actions:
                    return False

        # If the lookup succeeded, activate the corresponding action.
        if action:
            return self.activate_keydown_event(action, event)

        # Otherwise, dispatch the event to the active doc.
        return self._dispatch_fallthru_key_press_event(widget, event)