def keyPressEvent(self, event): if self.completer and self.completer.popup() and self.completer.popup( ).isVisible(): if event.key() in (Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab): event.ignore() #print("Event ignored") return text_cursor = self.textCursor() process_event = True if event.key() == Qt.Key_Tab: text_cursor.insertText(" " * self.tab_spaces) process_event = False if process_event: QPlainTextEdit.keyPressEvent(self, event) if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return: prev_line = self.get_previous_line() prev_line_leading_spaces = len(prev_line) - len( prev_line.lstrip(" ")) new_line_spaces = " " * prev_line_leading_spaces if prev_line.startswith(" "): text_cursor.insertText(new_line_spaces) arrow_pressed = event.key() in (Qt.Key_Right, Qt.Key_Left, Qt.Key_Up, Qt.Key_Down, Qt.Key_Shift, Qt.Key_Control) modifiers = QApplication.keyboardModifiers() if event.key() == Qt.Key_Space and modifiers == Qt.ControlModifier: arrow_pressed = False completionPrefix = self.textUnderCursor() #print(f"Completion prefix is {completionPrefix}") if len(completionPrefix) > 0 and not arrow_pressed: if not (completionPrefix in self.keywords and sum( k.startswith(completionPrefix) for k in self.keywords) == 1): self.completer.setCompletionPrefix(completionPrefix) popup = self.completer.popup() popup.setCurrentIndex(self.completer.completionModel().index( 0, 0)) cr = self.cursorRect() cr.setWidth(self.completer.popup().sizeHintForColumn(0) + self.completer.popup().verticalScrollBar( ).sizeHint().width()) self.completer.complete(cr) elif self.completer.popup(): self.completer.popup().hide()
def keyPressEvent(self, e): # if key == Qt.Key_Backspace or key == Qt.Key_Left or key == Qt.Key_Right or key == Qt.Key_Up or key == Qt.Key_Down: # return if self.m_localEchoEnabled: QPlainTextEdit.keyPressEvent(e) # key = e.text() if self.parent.alive: if e.text(): print('key: ', e.text) self.key_data_signal.emit(e.text()) elif e.key() in g_key_map: print('key: 0x%x' % g_key_map[e.key()]) self.key_data_signal.emit(chr(g_key_map[e.key()]))
def keyPressEvent(self, event): if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return: self.editing_finished(self.toPlainText()) self.last_text = self.toPlainText() else: QPlainTextEdit.keyPressEvent(self, event)