diff --git a/HotkeyTest/hottestwidget.cpp b/HotkeyTest/hottestwidget.cpp index 0574ddb..bab7069 100644 --- a/HotkeyTest/hottestwidget.cpp +++ b/HotkeyTest/hottestwidget.cpp @@ -11,6 +11,7 @@ HotTestWidget::HotTestWidget(QWidget *parent) : hotkey_5(new QHotkey(NULL)), thread4(new QThread(this)), thread5(new QThread(this)), + nativeHotkey(new QHotkey(this)), testHotkeys() { ui->setupUi(this); @@ -91,6 +92,10 @@ HotTestWidget::HotTestWidget(QWidget *parent) : this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this); connect(this->testHotkeys.last(), &QHotkey::activated, this->ui->hotkeyShiftAltKCheckBox_2, &QCheckBox::toggle); + + //native + connect(this->nativeHotkey, &QHotkey::activated, + this, &HotTestWidget::increase_native); } HotTestWidget::~HotTestWidget() @@ -217,3 +222,19 @@ void HotTestWidget::on_threadEnableCheckBox_clicked() this->ui->tabWidget->setCurrentIndex(0); } + +void HotTestWidget::on_registeredCheckBox_toggled(bool checked) +{ + if(checked) { + this->nativeHotkey->setNativeShortcut({ + (quint32)this->ui->nativeKeySpinBox->value(), + (quint32)this->ui->nativeModifiersSpinBox->value() + }, true); + } else + this->nativeHotkey->setRegistered(false); +} + +void HotTestWidget::increase_native() +{ + this->ui->nativeCount->display(this->ui->nativeCount->intValue() + 1); +} diff --git a/HotkeyTest/hottestwidget.h b/HotkeyTest/hottestwidget.h index 8aaa78e..d2f44e9 100644 --- a/HotkeyTest/hottestwidget.h +++ b/HotkeyTest/hottestwidget.h @@ -39,6 +39,9 @@ private slots: void on_groupBox_toggled(bool checked); void on_threadEnableCheckBox_clicked(); + void on_registeredCheckBox_toggled(bool checked); + void increase_native(); + private: Ui::HotTestWidget *ui; @@ -52,6 +55,8 @@ private: QThread *thread5; QList testHotkeys; + + QHotkey *nativeHotkey; }; #endif // HOTTESTWIDGET_H diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui index 745422f..542b61c 100644 --- a/HotkeyTest/hottestwidget.ui +++ b/HotkeyTest/hottestwidget.ui @@ -17,7 +17,7 @@ - 0 + 3 @@ -965,6 +965,194 @@ + + + Native Shortcut + + + + + + <html><head/><body><p>QHotkey allows you to set native shortcuts explicitly. These, of course, only work on the platform they were choosen for. All platform use special constants for their key codes and modifiers, which makes it pretty simple to use them from code. If you want to test them out here, google for the tables.</p><p>In most cases, you will not need to specify native shortcuts directly. However, as explaind on previos tabs, some shotcuts may not be creatable from Qt's key (e.g. Numblock numbers). In that case, you can set the directly.</p><p><span style=" text-decoration: underline;">Example: Ctrl+A</span></p><ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">Windows:</span> Key: <span style=" font-style:italic;">0x41</span>, Modifier: <span style=" font-style:italic;">0x02</span></li><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">X11:</span> Key: <span style=" font-style:italic;">0x1E</span>, Modifier: <span style=" font-style:italic;">0x02</span></li><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">OsX:</span> Key: <span style=" font-style:italic;">0x41</span>, Modifier: <span style=" font-style:italic;">0x02</span><span style=" text-decoration: underline;"><br/></span></li></ul></body></html> + + + true + + + + + + + + + Key: + + + + + + + 0x + + + 999999999 + + + 16 + + + + + + + Modifiers: + + + + + + + 0x + + + 999999999 + + + 16 + + + + + + + Count: + + + + + + + false + + + + + + + + 85 + 255 + 0 + + + + + + + 255 + 255 + 255 + + + + + + + 0 + 0 + 0 + + + + + + + + + 85 + 255 + 0 + + + + + + + 255 + 255 + 255 + + + + + + + 0 + 0 + 0 + + + + + + + + + 120 + 120 + 120 + + + + + + + 0 + 0 + 0 + + + + + + + 0 + 0 + 0 + + + + + + + + true + + + QFrame::Panel + + + QFrame::Sunken + + + QLCDNumber::Flat + + + + + + + Registered: + + + + + + + + + + @@ -996,12 +1184,12 @@ setEnabled(bool) - 36 - 52 + 31 + 45 - 375 - 62 + 417 + 43 @@ -1012,12 +1200,12 @@ setEnabled(bool) - 44 - 82 + 31 + 74 - 359 - 82 + 417 + 72 @@ -1028,12 +1216,12 @@ setEnabled(bool) - 52 - 113 + 31 + 103 - 375 - 120 + 417 + 101 @@ -1044,12 +1232,12 @@ setEnabled(bool) - 58 - 140 + 31 + 132 - 375 - 149 + 417 + 130 @@ -1060,12 +1248,12 @@ setEnabled(bool) - 28 - 163 + 31 + 161 - 375 - 178 + 417 + 159 @@ -1076,12 +1264,12 @@ setDisabled(bool) - 62 - 50 + 31 + 45 - 127 - 52 + 109 + 43 @@ -1092,12 +1280,12 @@ setDisabled(bool) - 83 - 79 + 31 + 74 - 161 - 81 + 109 + 72 @@ -1108,12 +1296,12 @@ setDisabled(bool) - 75 - 106 + 31 + 103 - 116 - 109 + 109 + 101 @@ -1124,12 +1312,12 @@ setDisabled(bool) - 69 - 135 + 31 + 132 - 117 - 141 + 109 + 130 @@ -1140,12 +1328,60 @@ setDisabled(bool) - 81 - 168 + 31 + 161 - 145 - 164 + 109 + 159 + + + + + registeredCheckBox + toggled(bool) + nativeCount + setEnabled(bool) + + + 93 + 326 + + + 150 + 349 + + + + + registeredCheckBox + toggled(bool) + nativeModifiersSpinBox + setDisabled(bool) + + + 108 + 327 + + + 111 + 305 + + + + + registeredCheckBox + toggled(bool) + nativeKeySpinBox + setDisabled(bool) + + + 183 + 327 + + + 194 + 279 diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 0c58eb0..d5aedea 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -28,6 +28,12 @@ QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister this->setShortcut(key, modifiers, autoRegister); } +QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) : + QHotkey(parent) +{ + this->setNativeShortcut(shortcut, autoRegister); +} + QHotkey::~QHotkey() { if(this->registered) @@ -52,6 +58,11 @@ Qt::KeyboardModifiers QHotkey::modifiers() const return this->mods; } +QHotkey::NativeShortcut QHotkey::currentNativeShortcut() const +{ + return this->nativeShortcut; +} + bool QHotkey::isRegistered() const { return this->registered; @@ -118,6 +129,32 @@ bool QHotkey::resetShortcut() return true; } +bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister) +{ + if(this->registered) { + if(autoRegister) { + if(!QHotkeyPrivate::instance()->removeShortcut(this)) + return false; + } else + return false; + } + + if(nativeShortcut.isValid()) { + this->key = Qt::Key_unknown; + this->mods = Qt::NoModifier; + this->nativeShortcut = nativeShortcut; + if(autoRegister) + return QHotkeyPrivate::instance()->addShortcut(this); + else + return true; + } else { + this->key = Qt::Key_unknown; + this->mods = Qt::NoModifier; + this->nativeShortcut = NativeShortcut(); + return true; + } +} + bool QHotkey::setRegistered(bool registered) { if(this->registered && !registered) diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index 84a640e..79533a2 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -42,6 +42,8 @@ public: explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR); //! Constructs a hotkey with a key and modifiers and optionally registers it explicit QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = Q_NULLPTR); + //! Constructs a hotkey from a native shortcut and optionally registers it + explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR); //! Destructor ~QHotkey(); @@ -54,6 +56,9 @@ public: //! READ-Accessor for QHotkey::shortcut - the modifiers only Qt::KeyboardModifiers modifiers() const; + // Get the current native shortcut + NativeShortcut currentNativeShortcut() const; + public slots: //! WRITE-Accessor for QHotkey::registered bool setRegistered(bool registered); @@ -65,6 +70,8 @@ public slots: //! RESET-Accessor for QHotkey::shortcut bool resetShortcut(); + bool setNativeShortcut(NativeShortcut nativeShortcut, bool autoRegister = false); + signals: //! Will be emitted if the shortcut is pressed void activated(QPrivateSignal);