def key_press(self, event: QKeyEvent): k = self.modifiers.get(event.modifiers()) if k is not None: key_seq = QKeySequence(k + event.key()) try: text = key_seq.toString() self.ctrl.setText(text) except: import traceback traceback.print_exc()
def validate_sequence(self): """Provide additional checks for accepting or rejecting shortcuts.""" if self.invalid_key_flag: self.update_warning(warning_type=INVALID_KEY) return for mod in MODIFIERS: non_mod = set(self.key_non_modifiers) non_mod.discard(mod) if mod in self.key_non_modifiers: self.key_non_modifiers.remove(mod) self.key_modifiers = self.key_modifiers - non_mod while u'' in self.key_text: self.key_text.remove(u'') self.key_text = [k.upper() for k in self.key_text] # Fix Backtab, Tab issue if os.name == 'nt': if Qt.Key_Backtab in self.key_non_modifiers: idx = self.key_non_modifiers.index(Qt.Key_Backtab) self.key_non_modifiers[idx] = Qt.Key_Tab if len(self.key_modifiers) == 0: # Filter single key allowed if self.key_non_modifiers[0] not in VALID_SINGLE_KEYS: return # Filter elif len(self.key_non_modifiers) > 1: return # QKeySequence accepts a maximum of 4 different sequences if len(self.keys) > 4: # Update warning self.update_warning(warning_type=SEQUENCE_LENGTH) return keys = [] for i in range(len(self.keys)): key_seq = 0 for m in self.key_modifiers: key_seq += MODIFIERS[m] key_seq += self.key_non_modifiers[i] keys.append(key_seq) sequence = QKeySequence(*keys) self.set_sequence(sequence.toString())
def validate_sequence(self): """Provide additional checks for accepting or rejecting shortcuts.""" if self.invalid_key_flag: self.update_warning(warning_type=INVALID_KEY) return for mod in MODIFIERS: non_mod = set(self.key_non_modifiers) non_mod.discard(mod) if mod in self.key_non_modifiers: self.key_non_modifiers.remove(mod) self.key_modifiers = self.key_modifiers - non_mod while u'' in self.key_text: self.key_text.remove(u'') self.key_text = [k.upper() for k in self.key_text] # Fix Backtab, Tab issue if os.name == 'nt': if Qt.Key_Backtab in self.key_non_modifiers: idx = self.key_non_modifiers.index(Qt.Key_Backtab) self.key_non_modifiers[idx] = Qt.Key_Tab if len(self.key_modifiers) == 0: # Filter single key allowed if self.key_non_modifiers[0] not in VALID_SINGLE_KEYS: return # Filter elif len(self.key_non_modifiers) > 1: return # QKeySequence accepts a maximum of 4 different sequences if len(self.keys) > 4: # Update warning self.update_warning(warning_type=SEQUENCE_LENGTH) return keys = [] for i in range(len(self.keys)): key_seq = 0 for m in self.key_modifiers: key_seq += MODIFIERS[m] key_seq += self.key_non_modifiers[i] keys.append(key_seq) sequence = QKeySequence(*keys) self.set_sequence(sequence.toString())
def shortcut_text(self, name_or_enum: str): # shortcut is always cast to string # but somtimes it is a key str "PgUp" and sometimes the QKeySequence.StandardKey enum value # https://doc.qt.io/qtforpython/PySide2/QtGui/QKeySequence.html # A QApplication instance must have been constructed before a QKeySequence is created; otherwise, your application may crash. try: enum_val = int(name_or_enum) ks = QKeySequence(QKeySequence.StandardKey(enum_val)) except ValueError: ks = QKeySequence(name_or_enum) text = ks.toString(QKeySequence.NativeText) return f' [{text}]' if text else ''
def add_shortcut_to_tooltip(action, context, name): """Add the shortcut associated with a given action to its tooltip""" if not hasattr(action, '_tooltip_backup'): # We store the original tooltip of the action without its associated # shortcut so that we can update the tooltip properly if shortcuts # are changed by the user over the course of the current session. # See spyder-ide/spyder#10726. action._tooltip_backup = action.toolTip() try: # Some shortcuts might not be assigned so we need to catch the error shortcut = CONF.get_shortcut(context=context, name=name) except (configparser.NoSectionError, configparser.NoOptionError): shortcut = None if shortcut: keyseq = QKeySequence(shortcut) # See: spyder-ide/spyder#12168 string = keyseq.toString(QKeySequence.NativeText) action.setToolTip(u'{0} ({1})'.format(action._tooltip_backup, string))