def event(self, ev): """Intercepts shortcurts and instead sends an equivalent event with the 'Alt' modifier, so that mnemonics works with just the key. Also sends a key press event with the 'Alt' key when this menu shows, so that mnemonics are underligned on windows. """ if ev.type() == QEvent.KeyPress and ev.key() == Qt.Key_Alt: return True if ev.type() == QEvent.ShortcutOverride and ev.modifiers() == Qt.NoModifier: actions = self.actions() + [a for child in self.findChildren(QWidget) for a in child.actions()] mnemonics = [QKeySequence.mnemonic(a.text()) for a in actions] key_seq = QKeySequence(Qt.ALT + ev.key()) if key_seq in mnemonics: ev = QKeyEvent(QEvent.KeyPress, ev.key(), Qt.AltModifier) qApp.postEvent(self, ev) # pylint: disable=undefined-variable return True if ev.type() == QEvent.Show: pev = QKeyEvent(QEvent.KeyPress, Qt.Key_Alt, Qt.NoModifier) qApp.postEvent(self, pev) # pylint: disable=undefined-variable return super().event(ev)
def event(self, event): """ overwritten from base class :param event: :return: """ if event.type() == QEvent.KeyPress: if event.key() == Qt.Key_Tab: if not self._compl.popup().isVisible(): self._compl.complete() self.nextCompletion(+1) return True if event.key() == Qt.Key_Backtab: if not self._compl.popup().isVisible(): self._compl.complete() self.nextCompletion(-1) return True if event.key() in [Qt.Key_Slash, Qt.Key_Backslash]: event = QKeyEvent(event.type(), event.key(), event.modifiers(), event.text()) return super().event(event)
def handle_wordcheck_key_events(self, e: QKeyEvent): """ Remaps key events to their Wordcheck mode equivalents. Only handles NoModifier and ShiftModifier events. :param e: The key event to remap. :return: """ if e.key() in [ Qt.Key_Delete, Qt.Key_Backspace, Qt.Key_Comma, Qt.Key_Period, Qt.Key_Semicolon, Qt.Key_Colon, Qt.Key_Less, Qt.Key_Greater ]: if e.type() == QKeyEvent.KeyPress: super().keyPressEvent(e) elif e.type() == QKeyEvent.KeyRelease: super().keyReleaseEvent(e) elif e.modifiers() == Qt.NoModifier: if e.key() in [Qt.Key_S, Qt.Key_H]: mapped_e = QKeyEvent(e.type(), Qt.Key_Left, Qt.AltModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_G, Qt.Key_L]: mapped_e = QKeyEvent(e.type(), Qt.Key_Right, Qt.AltModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_F, Qt.Key_J]: mapped_e = QKeyEvent(e.type(), Qt.Key_Down, Qt.KeypadModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_D, Qt.Key_K]: mapped_e = QKeyEvent(e.type(), Qt.Key_Up, Qt.KeypadModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_C, Qt.Key_N]: mapped_e = QKeyEvent(e.type(), Qt.Key_Left, Qt.KeypadModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_V, Qt.Key_M]: mapped_e = QKeyEvent(e.type(), Qt.Key_Right, Qt.KeypadModifier, autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() in [Qt.Key_R, Qt.Key_U ] and e.type() == QKeyEvent.KeyPress: if self.wordcheck_entry is not None: self.entry_idx += 1 self.next_word_replace() elif e.key() in [Qt.Key_E, Qt.Key_I ] and e.type() == QKeyEvent.KeyPress: if self.wordcheck_entry is not None: self.entry_idx -= 1 self.next_word_replace() elif e.key() in [Qt.Key_W, Qt.Key_O ] and e.type() == QKeyEvent.KeyPress: self.set_wordcheck_word_as_default() elif e.key() in [Qt.Key_A, Qt.Key_Z, Qt.Key_X]: if e.key() == Qt.Key_A: mapped_e = QKeyEvent(e.type(), Qt.Key_Semicolon, Qt.NoModifier, text=';', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() == Qt.Key_Z: mapped_e = QKeyEvent(e.type(), Qt.Key_Period, Qt.NoModifier, text='.', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) else: # Key_X mapped_e = QKeyEvent(e.type(), Qt.Key_Comma, Qt.NoModifier, text=',', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.modifiers() == Qt.ShiftModifier: if e.key() in [Qt.Key_A, Qt.Key_Z, Qt.Key_X]: if e.key() == Qt.Key_A: mapped_e = QKeyEvent(e.type(), Qt.Key_Colon, Qt.ShiftModifier, text=':', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) elif e.key() == Qt.Key_Z: mapped_e = QKeyEvent(e.type(), Qt.Key_Greater, Qt.ShiftModifier, text='>', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e) else: # Key_X mapped_e = QKeyEvent(e.type(), Qt.Key_Less, Qt.ShiftModifier, text='<', autorep=e.isAutoRepeat(), count=e.count()) QApplication.sendEvent(self, mapped_e)