'Custom keyboard behaviors
The leave() signal is emitted when `Up` is pressed and we\'re already
at the beginning of the text.  This allows the parent widget to
orchestrate some higher-level interaction, such as giving focus to
another widget.
When in the  middle of the first line and `Up` is pressed, the cursor
is moved to the beginning of the line.'

def keyPressEvent(self, event):

    widget = self.widget
    if (event.key() == Qt.Key_Up):
        cursor = widget.textCursor()
        position = cursor.position()
        if (position == 0):
            widget.leave.emit()
        elif (widget.value()[:position].count(u'\n') == 0):
            if (event.modifiers() & Qt.ShiftModifier):
                mode = QtGui.QTextCursor.KeepAnchor
            else:
                mode = QtGui.QTextCursor.MoveAnchor
            cursor.movePosition(QtGui.QTextCursor.StartOfLine, mode)
            widget.setTextCursor(cursor)
    return self.Base.keyPressEvent(widget, event)
