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_Escape | Esc key, typically used to cancel or exit |
Qt.Key_Tab | Tab key, used for focus switching |
Qt.Key_Backtab | Shift+Tab combination |
Qt.Key_Backspace | Backspace key |
Qt.Key_Return | Enter key on the main keyboard |
Qt.Key_Enter | Enter key on the numeric keypad |
Qt.Key_Insert | Insert key |
Qt.Key_Delete | Delete key |
Qt.Key_Pause | Pause/Break key |
Qt.Key_Print | Print Screen key |
Qt.Key_SysReq | System request key |
Qt.Key_Clear | Clear key |
Qt.Key_Home | Home key |
Qt.Key_End | End key |
Qt.Key_Left | Left arrow key |
Qt.Key_Up | Up arrow key |
Qt.Key_Right | Right arrow key |
Qt.Key_Down | Down arrow key |
Qt.Key_PageUp | Page Up key |
Qt.Key_PageDown | Page Down key |
Qt.Key_Shift | Shift key |
Qt.Key_Control | Ctrl key |
Qt.Key_Meta | Meta key |
Qt.Key_Alt | Alt key |
Qt.Key_AltGr | AltGr key |
Qt.Key_CapsLock | Caps Lock key |
Qt.Key_NumLock | Num Lock key |
Qt.Key_ScrollLock | Scroll Lock key |
Qt.Key_F1 | Function key F1 |
Qt.Key_F2 | Function key F2 |
Qt.Key_F3 | Function key F3 |
Qt.Key_F4 | Function key F4 |
Qt.Key_F5 | Function key F5 |
Qt.Key_F6 | Function key F6 |
Qt.Key_F7 | Function key F7 |
Qt.Key_F8 | Function key F8 |
Qt.Key_F9 | Function key F9 |
Qt.Key_F10 | Function key F10 |
Qt.Key_F11 | Function key F11 |
Qt.Key_F12 | Function key F12 |
Qt.Key_F13 | Function key F13 |
Qt.Key_F14 | Function key F14 |
Qt.Key_F15 | Function key F15 |
Qt.Key_F16 | Function key F16 |
Qt.Key_F17 | Function key F17 |
Qt.Key_F18 | Function key F18 |
Qt.Key_F19 | Function key F19 |
Qt.Key_F20 | Function key F20 |
Qt.Key_F21 | Function key F21 |
Qt.Key_F22 | Function key F22 |
Qt.Key_F23 | Function key F23 |
Qt.Key_F24 | Function key F24 |
Qt.Key_F25 | Function key F25 |
Qt.Key_F26 | Function key F26 |
Qt.Key_F27 | Function key F27 |
Qt.Key_F28 | Function key F28 |
Qt.Key_F29 | Function key F29 |
Qt.Key_F30 | Function key F30 |
Qt.Key_F31 | Function key F31 |
Qt.Key_F32 | Function key F32 |
Qt.Key_F33 | Function key F33 |
Qt.Key_F34 | Function key F34 |
Qt.Key_F35 | Function key F35 |
Qt.Key_Space | Space bar |
Qt.Key_A | Letter A |
Qt.Key_B | Letter B |
Qt.Key_C | Letter C |
Qt.Key_D | Letter D |
Qt.Key_E | Letter E |
Qt.Key_F | Letter F |
Qt.Key_G | Letter G |
Qt.Key_H | Letter H |
Qt.Key_I | Letter I |
Qt.Key_J | Letter J |
Qt.Key_K | Letter K |
Qt.Key_L | Letter L |
Qt.Key_M | Letter M |
Qt.Key_N | Letter N |
Qt.Key_O | Letter O |
Qt.Key_P | Letter P |
Qt.Key_Q | Letter Q |
Qt.Key_R | Letter R |
Qt.Key_S | Letter S |
Qt.Key_T | Letter T |
Qt.Key_U | Letter U |
Qt.Key_V | Letter V |
Qt.Key_W | Letter W |
Qt.Key_X | Letter X |
Qt.Key_Y | Letter Y |
Qt.Key_Z | Letter Z |
Qt.Key_0 | Digit 0 |
Qt.Key_1 | Digit 1 |
Qt.Key_2 | Digit 2 |
Qt.Key_3 | Digit 3 |
Qt.Key_4 | Digit 4 |
Qt.Key_5 | Digit 5 |
Qt.Key_6 | Digit 6 |
Qt.Key_7 | Digit 7 |
Qt.Key_8 | Digit 8 |
Qt.Key_9 | Digit 9 |
Qt.Key_VolumeDown | Volume Down |
Qt.Key_VolumeMute | Mute |
Qt.Key_VolumeUp | Volume Up |
Qt.Key_MediaPlay | Play |
Qt.Key_MediaStop | Stop |
Qt.Key_MediaPrevious | Previous Track |
Qt.Key_MediaNext | Next Track |
Qt.Key_MediaRecord | Record |
Qt.Key_MediaPause | Pause |
Qt.Key_MediaTogglePlayPause | Play/Pause Toggle |
Qt.Key_Help | Help |
Qt.Key_Menu | Menu key |
Qt.Key_Undo | Undo |
Qt.Key_Redo | Redo |
Qt.Key_Find | Find |
Qt.Key_New | New |
Qt.Key_Open | Open |
Qt.Key_Save | Save |
Qt.Key_Close | Close |
Qt.Key_Copy | Copy |
Qt.Key_Cut | Cut |
Qt.Key_Paste | Paste |
Qt.Key_ZoomIn | Zoom In |
Qt.Key_ZoomOut | Zoom Out |
Qt.Key_Sleep | Sleep |
Qt.Key_PowerDown | Power Down |
Qt.Key_Hibernate | Hibernate |
Qt.Key_WakeUp | Wake Up |
Qt.Key_Eject | Eject |
Qt.Key_unknown | Unknown Key |
QKeySequence Constants
QKeySequence provides a series of predefined standard shortcut constants for cross-platform keyboard shortcuts.
| Constant | Windows/Linux | macOS | Description |
|---|---|---|---|
QKeySequence.Copy | Ctrl+C | Cmd+C | Copy |
QKeySequence.Cut | Ctrl+X | Cmd+X | Cut |
QKeySequence.Paste | Ctrl+V | Cmd+V | Paste |
QKeySequence.Undo | Ctrl+Z | Cmd+Z | Undo |
QKeySequence.Redo | Ctrl+Y | Cmd+Shift+Z | Redo |
QKeySequence.Save | Ctrl+S | Cmd+S | Save |
QKeySequence.SaveAs | Ctrl+Shift+S | Cmd+Shift+S | Save As |
QKeySequence.Open | Ctrl+O | Cmd+O | Open |
QKeySequence.New | Ctrl+N | Cmd+N | New |
QKeySequence.Close | Ctrl+W | Cmd+W | Close |
QKeySequence.Quit | Ctrl+Q | Cmd+Q | Quit |
QKeySequence.Print | Ctrl+P | Cmd+P | |
QKeySequence.Delete | Del | Del | Delete |
QKeySequence.Find | Ctrl+F | Cmd+F | Find |
QKeySequence.FindNext | F3 | Cmd+G | Find Next |
QKeySequence.FindPrevious | Shift+F3 | Cmd+Shift+G | Find Previous |
QKeySequence.Replace | Ctrl+H | Cmd+H | Replace |
QKeySequence.SelectAll | Ctrl+A | Cmd+A | Select All |
QKeySequence.Deselect | Ctrl+Shift+A | Cmd+Shift+A | Deselect |
QKeySequence.DeleteEndOfWord | Ctrl+Del | Alt+Del | Delete to End of Word |
QKeySequence.DeleteStartOfWord | Ctrl+Backspace | Alt+Backspace | Delete to Start of Word |
QKeySequence.MoveToNextWord | Ctrl+Right | Alt+Right | Move to Next Word |
QKeySequence.MoveToPreviousWord | Ctrl+Left | Alt+Left | Move to Previous Word |
QKeySequence.MoveToNextLine | Down | Down | Move to Next Line |
QKeySequence.MoveToPreviousLine | Up | Up | Move to Previous Line |
QKeySequence.MoveToNextChar | Right | Right | Move to Next Character |
QKeySequence.MoveToPreviousChar | Left | Left | Move to Previous Character |
QKeySequence.MoveToStartOfLine | Home | Cmd+Left | Move to Start of Line |
QKeySequence.MoveToEndOfLine | End | Cmd+Right | Move to End of Line |
QKeySequence.MoveToStartOfBlock | Ctrl+Up | Cmd+Up | Move to Start of Block |
QKeySequence.MoveToEndOfBlock | Ctrl+Down | Cmd+Down | Move to End of Block |
QKeySequence.MoveToStartOfDocument | Ctrl+Home | Cmd+Up | Move to Start of Document |
QKeySequence.MoveToEndOfDocument | Ctrl+End | Cmd+Down | Move to End of Document |
QKeySequence.SelectNextWord | Ctrl+Shift+Right | Alt+Shift+Right | Select Next Word |
QKeySequence.SelectPreviousWord | Ctrl+Shift+Left | Alt+Shift+Left | Select Previous Word |
QKeySequence.SelectNextLine | Shift+Down | Shift+Down | Select Next Line |
QKeySequence.SelectPreviousLine | Shift+Up | Shift+Up | Select Previous Line |
QKeySequence.SelectNextChar | Shift+Right | Shift+Right | Select Next Character |
QKeySequence.SelectPreviousChar | Shift+Left | Shift+Left | Select Previous Character |
QKeySequence.SelectStartOfLine | Shift+Home | Cmd+Shift+Left | Select to Start of Line |
QKeySequence.SelectEndOfLine | Shift+End | Cmd+Shift+Right | Select to End of Line |
QKeySequence.SelectStartOfBlock | Ctrl+Shift+Up | Cmd+Shift+Up | Select to Start of Block |
QKeySequence.SelectEndOfBlock | Ctrl+Shift+Down | Cmd+Shift+Down | Select to End of Block |
QKeySequence.SelectStartOfDocument | Ctrl+Shift+Home | Cmd+Shift+Up | Select to Start of Document |
QKeySequence.SelectEndOfDocument | Ctrl+Shift+End | Cmd+Shift+Down | Select to End of Document |
QKeySequence.DeleteCompleteLine | Ctrl+Shift+Del | Cmd+Shift+Del | Delete Complete Line |
QKeySequence.InsertParagraphSeparator | Enter | Enter | Insert Paragraph Separator |
QKeySequence.InsertLineSeparator | Shift+Enter | Shift+Enter | Insert Line Separator |
QKeySequence.Backspace | Backspace | Backspace | Backspace Delete |
QKeySequence.Help | F1 | Cmd+? | Help |
QKeySequence.WhatsThis | Shift+F1 | Shift+F1 | What's This |
QKeySequence.Preferences | Ctrl+, | Cmd+, | Preferences |
QKeySequence.ZoomIn | Ctrl++ | Cmd++ | Zoom In |
QKeySequence.ZoomOut | Ctrl+- | Cmd+- | Zoom Out |
QKeySequence.FullScreen | F11 | Cmd+Ctrl+F | Full Screen |
QKeySequence.Refresh | F5 | Cmd+R | Refresh |
QKeySequence.Forward | Alt+Right | Cmd+] | Forward |
QKeySequence.Back | Alt+Left | Cmd+[ | Back |