In PySide6, the QShortcut class provides a convenient way to set keyboard shortcuts for applications, thereby enhancing user interaction efficiency. By binding shortcuts to specific actions or functions, users can quickly perform common operations without relying on menus or buttons.

Basic Usage of QShortcut

The following example demonstrates how to set shortcuts for a simple window, printing a message when Ctrl + P is pressed and exiting the program when ESC is pressed.

import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtGui import QShortcut
from PySide6.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QShortcut Example")
        self.setGeometry(100, 100, 400, 300)
        
        # Create shortcut, bind Ctrl + P
        sc1 = QShortcut(self)
        sc1.setKey(Qt.CTRL | Qt.Key_P)
        sc1.activated.connect(self.ctrl_p)

        # Bind ESC
        sc2 = QShortcut("ESC",self)
        sc2.activated.connect(QApplication.exit)

    def ctrl_p(self):
        print("Ctrl + P pressed")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Common Initialization Methods for QShortcut

QShortcut has multiple initialization methods:

from PySide6.QtWidgets import QShortcut, QWidget
from PySide6.QtGui import QKeySequence
from PySide6.QtCore import Qt

# Method 1: Set shortcut using a string
shortcut = QShortcut(QKeySequence("Ctrl+S"), parent_widget)

# Method 2: Use standard keys
shortcut = QShortcut(QKeySequence.Save, parent_widget)

# Method 3: Use Qt enumeration combinations
shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key.Key_S), parent_widget)

# Method 4: Create first, then set
shortcut = QShortcut(parent_widget)
shortcut.setKey(QKeySequence("Ctrl+S"))

You can also directly use strings:

shortcut = QShortcut("Ctrl+S", parent_widget)

activated Signal

The activated signal of QShortcut is triggered when the shortcut is pressed and can be connected to any callable function or method.

shortcut.activated.connect(function_name)

activatedAmbiguously Signal

When a shortcut key sequence is registered by multiple QShortcut objects simultaneously, pressing that shortcut will trigger the activatedAmbiguously signal for all related shortcuts, but not the activated signal.

sc1 = QShortcut("ESC",self)
sc1.activated.connect(QApplication.exit)
sc1.activatedAmbiguously.connect(lambda:print("Key conflict"))

sc2 = QShortcut("ESC",self)
sc2.activated.connect(QApplication.exit)

Qt.Key Enumeration Constants

Qt.Key is an enumeration type in the Qt framework used to represent keyboard keys.

Enumeration Constant Brief Description
Qt.Key_EscapeEsc key, typically used to cancel or exit
Qt.Key_TabTab key, used for focus switching
Qt.Key_BacktabShift+Tab combination
Qt.Key_BackspaceBackspace key
Qt.Key_ReturnEnter key on the main keyboard
Qt.Key_EnterEnter key on the numeric keypad
Qt.Key_InsertInsert key
Qt.Key_DeleteDelete key
Qt.Key_PausePause/Break key
Qt.Key_PrintPrint Screen key
Qt.Key_SysReqSystem request key
Qt.Key_ClearClear key
Qt.Key_HomeHome key
Qt.Key_EndEnd key
Qt.Key_LeftLeft arrow key
Qt.Key_UpUp arrow key
Qt.Key_RightRight arrow key
Qt.Key_DownDown arrow key
Qt.Key_PageUpPage Up key
Qt.Key_PageDownPage Down key
Qt.Key_ShiftShift key
Qt.Key_ControlCtrl key
Qt.Key_MetaMeta key
Qt.Key_AltAlt key
Qt.Key_AltGrAltGr key
Qt.Key_CapsLockCaps Lock key
Qt.Key_NumLockNum Lock key
Qt.Key_ScrollLockScroll Lock key
Qt.Key_F1Function key F1
Qt.Key_F2Function key F2
Qt.Key_F3Function key F3
Qt.Key_F4Function key F4
Qt.Key_F5Function key F5
Qt.Key_F6Function key F6
Qt.Key_F7Function key F7
Qt.Key_F8Function key F8
Qt.Key_F9Function key F9
Qt.Key_F10Function key F10
Qt.Key_F11Function key F11
Qt.Key_F12Function key F12
Qt.Key_F13Function key F13
Qt.Key_F14Function key F14
Qt.Key_F15Function key F15
Qt.Key_F16Function key F16
Qt.Key_F17Function key F17
Qt.Key_F18Function key F18
Qt.Key_F19Function key F19
Qt.Key_F20Function key F20
Qt.Key_F21Function key F21
Qt.Key_F22Function key F22
Qt.Key_F23Function key F23
Qt.Key_F24Function key F24
Qt.Key_F25Function key F25
Qt.Key_F26Function key F26
Qt.Key_F27Function key F27
Qt.Key_F28Function key F28
Qt.Key_F29Function key F29
Qt.Key_F30Function key F30
Qt.Key_F31Function key F31
Qt.Key_F32Function key F32
Qt.Key_F33Function key F33
Qt.Key_F34Function key F34
Qt.Key_F35Function key F35
Qt.Key_SpaceSpace bar
Qt.Key_ALetter A
Qt.Key_BLetter B
Qt.Key_CLetter C
Qt.Key_DLetter D
Qt.Key_ELetter E
Qt.Key_FLetter F
Qt.Key_GLetter G
Qt.Key_HLetter H
Qt.Key_ILetter I
Qt.Key_JLetter J
Qt.Key_KLetter K
Qt.Key_LLetter L
Qt.Key_MLetter M
Qt.Key_NLetter N
Qt.Key_OLetter O
Qt.Key_PLetter P
Qt.Key_QLetter Q
Qt.Key_RLetter R
Qt.Key_SLetter S
Qt.Key_TLetter T
Qt.Key_ULetter U
Qt.Key_VLetter V
Qt.Key_WLetter W
Qt.Key_XLetter X
Qt.Key_YLetter Y
Qt.Key_ZLetter Z
Qt.Key_0Digit 0
Qt.Key_1Digit 1
Qt.Key_2Digit 2
Qt.Key_3Digit 3
Qt.Key_4Digit 4
Qt.Key_5Digit 5
Qt.Key_6Digit 6
Qt.Key_7Digit 7
Qt.Key_8Digit 8
Qt.Key_9Digit 9
Qt.Key_VolumeDownVolume Down
Qt.Key_VolumeMuteMute
Qt.Key_VolumeUpVolume Up
Qt.Key_MediaPlayPlay
Qt.Key_MediaStopStop
Qt.Key_MediaPreviousPrevious Track
Qt.Key_MediaNextNext Track
Qt.Key_MediaRecordRecord
Qt.Key_MediaPausePause
Qt.Key_MediaTogglePlayPausePlay/Pause Toggle
Qt.Key_HelpHelp
Qt.Key_MenuMenu key
Qt.Key_UndoUndo
Qt.Key_RedoRedo
Qt.Key_FindFind
Qt.Key_NewNew
Qt.Key_OpenOpen
Qt.Key_SaveSave
Qt.Key_CloseClose
Qt.Key_CopyCopy
Qt.Key_CutCut
Qt.Key_PastePaste
Qt.Key_ZoomInZoom In
Qt.Key_ZoomOutZoom Out
Qt.Key_SleepSleep
Qt.Key_PowerDownPower Down
Qt.Key_HibernateHibernate
Qt.Key_WakeUpWake Up
Qt.Key_EjectEject
Qt.Key_unknownUnknown Key

QKeySequence Constants

QKeySequence provides a series of predefined standard shortcut constants for cross-platform keyboard shortcuts.

Constant Windows/Linux macOS Description
QKeySequence.CopyCtrl+CCmd+CCopy
QKeySequence.CutCtrl+XCmd+XCut
QKeySequence.PasteCtrl+VCmd+VPaste
QKeySequence.UndoCtrl+ZCmd+ZUndo
QKeySequence.RedoCtrl+YCmd+Shift+ZRedo
QKeySequence.SaveCtrl+SCmd+SSave
QKeySequence.SaveAsCtrl+Shift+SCmd+Shift+SSave As
QKeySequence.OpenCtrl+OCmd+OOpen
QKeySequence.NewCtrl+NCmd+NNew
QKeySequence.CloseCtrl+WCmd+WClose
QKeySequence.QuitCtrl+QCmd+QQuit
QKeySequence.PrintCtrl+PCmd+PPrint
QKeySequence.DeleteDelDelDelete
QKeySequence.FindCtrl+FCmd+FFind
QKeySequence.FindNextF3Cmd+GFind Next
QKeySequence.FindPreviousShift+F3Cmd+Shift+GFind Previous
QKeySequence.ReplaceCtrl+HCmd+HReplace
QKeySequence.SelectAllCtrl+ACmd+ASelect All
QKeySequence.DeselectCtrl+Shift+ACmd+Shift+ADeselect
QKeySequence.DeleteEndOfWordCtrl+DelAlt+DelDelete to End of Word
QKeySequence.DeleteStartOfWordCtrl+BackspaceAlt+BackspaceDelete to Start of Word
QKeySequence.MoveToNextWordCtrl+RightAlt+RightMove to Next Word
QKeySequence.MoveToPreviousWordCtrl+LeftAlt+LeftMove to Previous Word
QKeySequence.MoveToNextLineDownDownMove to Next Line
QKeySequence.MoveToPreviousLineUpUpMove to Previous Line
QKeySequence.MoveToNextCharRightRightMove to Next Character
QKeySequence.MoveToPreviousCharLeftLeftMove to Previous Character
QKeySequence.MoveToStartOfLineHomeCmd+LeftMove to Start of Line
QKeySequence.MoveToEndOfLineEndCmd+RightMove to End of Line
QKeySequence.MoveToStartOfBlockCtrl+UpCmd+UpMove to Start of Block
QKeySequence.MoveToEndOfBlockCtrl+DownCmd+DownMove to End of Block
QKeySequence.MoveToStartOfDocumentCtrl+HomeCmd+UpMove to Start of Document
QKeySequence.MoveToEndOfDocumentCtrl+EndCmd+DownMove to End of Document
QKeySequence.SelectNextWordCtrl+Shift+RightAlt+Shift+RightSelect Next Word
QKeySequence.SelectPreviousWordCtrl+Shift+LeftAlt+Shift+LeftSelect Previous Word
QKeySequence.SelectNextLineShift+DownShift+DownSelect Next Line
QKeySequence.SelectPreviousLineShift+UpShift+UpSelect Previous Line
QKeySequence.SelectNextCharShift+RightShift+RightSelect Next Character
QKeySequence.SelectPreviousCharShift+LeftShift+LeftSelect Previous Character
QKeySequence.SelectStartOfLineShift+HomeCmd+Shift+LeftSelect to Start of Line
QKeySequence.SelectEndOfLineShift+EndCmd+Shift+RightSelect to End of Line
QKeySequence.SelectStartOfBlockCtrl+Shift+UpCmd+Shift+UpSelect to Start of Block
QKeySequence.SelectEndOfBlockCtrl+Shift+DownCmd+Shift+DownSelect to End of Block
QKeySequence.SelectStartOfDocumentCtrl+Shift+HomeCmd+Shift+UpSelect to Start of Document
QKeySequence.SelectEndOfDocumentCtrl+Shift+EndCmd+Shift+DownSelect to End of Document
QKeySequence.DeleteCompleteLineCtrl+Shift+DelCmd+Shift+DelDelete Complete Line
QKeySequence.InsertParagraphSeparatorEnterEnterInsert Paragraph Separator
QKeySequence.InsertLineSeparatorShift+EnterShift+EnterInsert Line Separator
QKeySequence.BackspaceBackspaceBackspaceBackspace Delete
QKeySequence.HelpF1Cmd+?Help
QKeySequence.WhatsThisShift+F1Shift+F1What's This
QKeySequence.PreferencesCtrl+,Cmd+,Preferences
QKeySequence.ZoomInCtrl++Cmd++Zoom In
QKeySequence.ZoomOutCtrl+-Cmd+-Zoom Out
QKeySequence.FullScreenF11Cmd+Ctrl+FFull Screen
QKeySequence.RefreshF5Cmd+RRefresh
QKeySequence.ForwardAlt+RightCmd+]Forward
QKeySequence.BackAlt+LeftCmd+[Back