(self, event)
QPlainTextEdit.keyPressEvent() implementation.
        Catch events, which may not be catched with QShortcut and call slots
QPlainTextEdit.keyPressEvent() implementation.
        Catch events, which may not be catched with QShortcut and call slots
QPlainTextEdit . keyPressEvent () implementation . Catch events which may not be catched with QShortcut and call slots
def keyPressEvent(self, event):
        """QPlainTextEdit.keyPressEvent() implementation.
        Catch events, which may not be catched with QShortcut and call slots
        """
        self._lastKeyPressProcessedByParent = False

        cursor = self.textCursor()

        def shouldUnindentWithBackspace():
            text = cursor.block().text()
            spaceAtStartLen = len(text) - len(text.lstrip())

            return self.textBeforeCursor().endswith(self._indenter.text()) and \
                   not cursor.hasSelection() and \
                   cursor.positionInBlock() == spaceAtStartLen

        def atEnd():
            return cursor.positionInBlock() == cursor.block().length() - 1

        def shouldAutoIndent(event):
            return atEnd() and \
                   event.text() and \
                   event.text() in self._indenter.triggerCharacters()

        def backspaceOverwrite():
            with self:
                cursor.deletePreviousChar()
                cursor.insertText(' ')
                setPositionInBlock(cursor, cursor.positionInBlock() - 1)
                self.setTextCursor(cursor)

        def typeOverwrite(text):
            """QPlainTextEdit records text input in replace mode as 2 actions:
            delete char, and type char. Actions are undone separately. This is
            workaround for the Qt bug"""
            with self:
                if not atEnd():
                    cursor.deleteChar()
                cursor.insertText(text)

        # mac specific shortcuts,
        if sys.platform == 'darwin':
            # it seems weird to delete line on CTRL+Backspace on Windows,
            # that's for deleting words. But Mac's CMD maps to Qt's CTRL.
            if event.key() == Qt.Key_Backspace and event.modifiers() == Qt.ControlModifier:
                self.deleteLineAction.trigger()
                event.accept()
                return
        if event.matches(QKeySequence.InsertLineSeparator):
            event.ignore()
            return
        elif event.matches(QKeySequence.InsertParagraphSeparator):
            if self._vim is not None:
                if self._vim.keyPressEvent(event):
                    return
            self._insertNewBlock()
        elif event.matches(QKeySequence.Copy) and self._rectangularSelection.isActive():
            self._rectangularSelection.copy()
        elif event.matches(QKeySequence.Cut) and self._rectangularSelection.isActive():
            self._rectangularSelection.cut()
        elif self._rectangularSelection.isDeleteKeyEvent(event):
            self._rectangularSelection.delete()
        elif event.key() == Qt.Key_Insert and event.modifiers() == Qt.NoModifier:
            if self._vim is not None:
                self._vim.keyPressEvent(event)
            else:
                self.setOverwriteMode(not self.overwriteMode())
        elif event.key() == Qt.Key_Backspace and \
                shouldUnindentWithBackspace():
            self._indenter.onShortcutUnindentWithBackspace()
        elif event.key() == Qt.Key_Backspace and \
                not cursor.hasSelection() and \
                self.overwriteMode() and \
                cursor.positionInBlock() > 0:
            backspaceOverwrite()
        elif self.overwriteMode() and \
                event.text() and \
                isChar(event) and \
                not cursor.hasSelection() and \
                cursor.positionInBlock() < cursor.block().length():
            typeOverwrite(event.text())
            if self._vim is not None:
                self._vim.keyPressEvent(event)
        elif event.matches(QKeySequence.MoveToStartOfLine):
            if self._vim is not None and \
                    self._vim.keyPressEvent(event):
                return
            else:
                self._onShortcutHome(select=False)
        elif event.matches(QKeySequence.SelectStartOfLine):
            self._onShortcutHome(select=True)
        elif self._rectangularSelection.isExpandKeyEvent(event):
            self._rectangularSelection.onExpandKeyEvent(event)
        elif shouldAutoIndent(event):
            with self:
                super().keyPressEvent(event)
                self._indenter.autoIndentBlock(cursor.block(), event.text())
        else:
            if self._vim is not None:
                if self._vim.keyPressEvent(event):
                    return

            # make action shortcuts override keyboard events (non-default Qt behaviour)
            for action in self.actions():
                seq = action.shortcut()
                if seq.count() == 1 and seq[0] == event.key() | int(event.modifiers()):
                    action.trigger()
                    break
            else:
                self._lastKeyPressProcessedByParent = True
                super().keyPressEvent(event)

        if event.key() == Qt.Key_Escape:
            event.accept()
def keyPressEvent ( self , event ) : self . _lastKeyPressProcessedByParent = False cursor = self . textCursor ( ) def shouldUnindentWithBackspace ( ) : text = cursor . block ( ) . text ( ) spaceAtStartLen = len ( text ) - len ( text . lstrip ( ) ) return self . textBeforeCursor ( ) . endswith ( self . _indenter . text ( ) ) and not cursor . hasSelection ( ) and cursor . positionInBlock ( ) == spaceAtStartLen def atEnd ( ) : return cursor . positionInBlock ( ) == cursor . block ( ) . length ( ) - 1 def shouldAutoIndent ( event ) : return atEnd ( ) and event . text ( ) and event . text ( ) in self . _indenter . triggerCharacters ( ) def backspaceOverwrite ( ) : with self : cursor . deletePreviousChar ( ) cursor . insertText ( ' ' ) setPositionInBlock ( cursor , cursor . positionInBlock ( ) - 1 ) self . setTextCursor ( cursor ) def typeOverwrite ( text ) : """QPlainTextEdit records text input in replace mode as 2 actions:
            delete char, and type char. Actions are undone separately. This is
            workaround for the Qt bug""" with self : if not atEnd ( ) : cursor . deleteChar ( ) cursor . insertText ( text ) # mac specific shortcuts, if sys . platform == 'darwin' : # it seems weird to delete line on CTRL+Backspace on Windows, # that's for deleting words. But Mac's CMD maps to Qt's CTRL. if event . key ( ) == Qt . Key_Backspace and event . modifiers ( ) == Qt . ControlModifier : self . deleteLineAction . trigger ( ) event . accept ( ) return if event . matches ( QKeySequence . InsertLineSeparator ) : event . ignore ( ) return elif event . matches ( QKeySequence . InsertParagraphSeparator ) : if self . _vim is not None : if self . _vim . keyPressEvent ( event ) : return self . _insertNewBlock ( ) elif event . matches ( QKeySequence . Copy ) and self . _rectangularSelection . isActive ( ) : self . _rectangularSelection . copy ( ) elif event . matches ( QKeySequence . Cut ) and self . _rectangularSelection . isActive ( ) : self . _rectangularSelection . cut ( ) elif self . _rectangularSelection . isDeleteKeyEvent ( event ) : self . _rectangularSelection . delete ( ) elif event . key ( ) == Qt . Key_Insert and event . modifiers ( ) == Qt . NoModifier : if self . _vim is not None : self . _vim . keyPressEvent ( event ) else : self . setOverwriteMode ( not self . overwriteMode ( ) ) elif event . key ( ) == Qt . Key_Backspace and shouldUnindentWithBackspace ( ) : self . _indenter . onShortcutUnindentWithBackspace ( ) elif event . key ( ) == Qt . Key_Backspace and not cursor . hasSelection ( ) and self . overwriteMode ( ) and cursor . positionInBlock ( ) > 0 : backspaceOverwrite ( ) elif self . overwriteMode ( ) and event . text ( ) and isChar ( event ) and not cursor . hasSelection ( ) and cursor . positionInBlock ( ) < cursor . block ( ) . length ( ) : typeOverwrite ( event . text ( ) ) if self . _vim is not None : self . _vim . keyPressEvent ( event ) elif event . matches ( QKeySequence . MoveToStartOfLine ) : if self . _vim is not None and self . _vim . keyPressEvent ( event ) : return else : self . _onShortcutHome ( select = False ) elif event . matches ( QKeySequence . SelectStartOfLine ) : self . _onShortcutHome ( select = True ) elif self . _rectangularSelection . isExpandKeyEvent ( event ) : self . _rectangularSelection . onExpandKeyEvent ( event ) elif shouldAutoIndent ( event ) : with self : super ( ) . keyPressEvent ( event ) self . _indenter . autoIndentBlock ( cursor . block ( ) , event . text ( ) ) else : if self . _vim is not None : if self . _vim . keyPressEvent ( event ) : return # make action shortcuts override keyboard events (non-default Qt behaviour) for action in self . actions ( ) : seq = action . shortcut ( ) if seq . count ( ) == 1 and seq [ 0 ] == event . key ( ) | int ( event . modifiers ( ) ) : action . trigger ( ) break else : self . _lastKeyPressProcessedByParent = True super ( ) . keyPressEvent ( event ) if event . key ( ) == Qt . Key_Escape : event . accept ( )