added native shortcut support (win)

This commit is contained in:
Skycoder42
2016-12-04 16:49:53 +01:00
parent 5cc4b7bf2d
commit 686abe219a
5 changed files with 347 additions and 41 deletions
+21
View File
@@ -11,6 +11,7 @@ HotTestWidget::HotTestWidget(QWidget *parent) :
hotkey_5(new QHotkey(NULL)), hotkey_5(new QHotkey(NULL)),
thread4(new QThread(this)), thread4(new QThread(this)),
thread5(new QThread(this)), thread5(new QThread(this)),
nativeHotkey(new QHotkey(this)),
testHotkeys() testHotkeys()
{ {
ui->setupUi(this); ui->setupUi(this);
@@ -91,6 +92,10 @@ HotTestWidget::HotTestWidget(QWidget *parent) :
this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this); this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this);
connect(this->testHotkeys.last(), &QHotkey::activated, connect(this->testHotkeys.last(), &QHotkey::activated,
this->ui->hotkeyShiftAltKCheckBox_2, &QCheckBox::toggle); this->ui->hotkeyShiftAltKCheckBox_2, &QCheckBox::toggle);
//native
connect(this->nativeHotkey, &QHotkey::activated,
this, &HotTestWidget::increase_native);
} }
HotTestWidget::~HotTestWidget() HotTestWidget::~HotTestWidget()
@@ -217,3 +222,19 @@ void HotTestWidget::on_threadEnableCheckBox_clicked()
this->ui->tabWidget->setCurrentIndex(0); 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);
}
+5
View File
@@ -39,6 +39,9 @@ private slots:
void on_groupBox_toggled(bool checked); void on_groupBox_toggled(bool checked);
void on_threadEnableCheckBox_clicked(); void on_threadEnableCheckBox_clicked();
void on_registeredCheckBox_toggled(bool checked);
void increase_native();
private: private:
Ui::HotTestWidget *ui; Ui::HotTestWidget *ui;
@@ -52,6 +55,8 @@ private:
QThread *thread5; QThread *thread5;
QList<QHotkey*> testHotkeys; QList<QHotkey*> testHotkeys;
QHotkey *nativeHotkey;
}; };
#endif // HOTTESTWIDGET_H #endif // HOTTESTWIDGET_H
+277 -41
View File
@@ -17,7 +17,7 @@
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>3</number>
</property> </property>
<widget class="QWidget" name="playTab"> <widget class="QWidget" name="playTab">
<attribute name="title"> <attribute name="title">
@@ -965,6 +965,194 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="nativeTab">
<attribute name="title">
<string>Native Shortcut</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,1">
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Example: Ctrl+A&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;&quot;&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Windows:&lt;/span&gt; Key: &lt;span style=&quot; font-style:italic;&quot;&gt;0x41&lt;/span&gt;, Modifier: &lt;span style=&quot; font-style:italic;&quot;&gt;0x02&lt;/span&gt;&lt;/li&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;X11:&lt;/span&gt; Key: &lt;span style=&quot; font-style:italic;&quot;&gt;0x1E&lt;/span&gt;, Modifier: &lt;span style=&quot; font-style:italic;&quot;&gt;0x02&lt;/span&gt;&lt;/li&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;OsX:&lt;/span&gt; Key: &lt;span style=&quot; font-style:italic;&quot;&gt;0x41&lt;/span&gt;, Modifier: &lt;span style=&quot; font-style:italic;&quot;&gt;0x02&lt;/span&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;&lt;br/&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="keyLabel">
<property name="text">
<string>Key:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="nativeKeySpinBox">
<property name="prefix">
<string>0x</string>
</property>
<property name="maximum">
<number>999999999</number>
</property>
<property name="displayIntegerBase">
<number>16</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="modifiersLabel">
<property name="text">
<string>Modifiers:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="nativeModifiersSpinBox">
<property name="prefix">
<string>0x</string>
</property>
<property name="maximum">
<number>999999999</number>
</property>
<property name="displayIntegerBase">
<number>16</number>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="countLabel">
<property name="text">
<string>Count:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLCDNumber" name="nativeCount">
<property name="enabled">
<bool>false</bool>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>85</red>
<green>255</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>85</red>
<green>255</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>120</red>
<green>120</green>
<blue>120</blue>
</color>
</brush>
</colorrole>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="segmentStyle">
<enum>QLCDNumber::Flat</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="registeredLabel">
<property name="text">
<string>Registered:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="registeredCheckBox"/>
</item>
</layout>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>
@@ -996,12 +1184,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>36</x> <x>31</x>
<y>52</y> <y>45</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>375</x> <x>417</x>
<y>62</y> <y>43</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1012,12 +1200,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>44</x> <x>31</x>
<y>82</y> <y>74</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>359</x> <x>417</x>
<y>82</y> <y>72</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1028,12 +1216,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>52</x> <x>31</x>
<y>113</y> <y>103</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>375</x> <x>417</x>
<y>120</y> <y>101</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1044,12 +1232,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>58</x> <x>31</x>
<y>140</y> <y>132</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>375</x> <x>417</x>
<y>149</y> <y>130</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1060,12 +1248,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>28</x> <x>31</x>
<y>163</y> <y>161</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>375</x> <x>417</x>
<y>178</y> <y>159</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1076,12 +1264,12 @@
<slot>setDisabled(bool)</slot> <slot>setDisabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>62</x> <x>31</x>
<y>50</y> <y>45</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>127</x> <x>109</x>
<y>52</y> <y>43</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1092,12 +1280,12 @@
<slot>setDisabled(bool)</slot> <slot>setDisabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>83</x> <x>31</x>
<y>79</y> <y>74</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>161</x> <x>109</x>
<y>81</y> <y>72</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1108,12 +1296,12 @@
<slot>setDisabled(bool)</slot> <slot>setDisabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>75</x> <x>31</x>
<y>106</y> <y>103</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>116</x> <x>109</x>
<y>109</y> <y>101</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1124,12 +1312,12 @@
<slot>setDisabled(bool)</slot> <slot>setDisabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>69</x> <x>31</x>
<y>135</y> <y>132</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>117</x> <x>109</x>
<y>141</y> <y>130</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -1140,12 +1328,60 @@
<slot>setDisabled(bool)</slot> <slot>setDisabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>81</x> <x>31</x>
<y>168</y> <y>161</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>145</x> <x>109</x>
<y>164</y> <y>159</y>
</hint>
</hints>
</connection>
<connection>
<sender>registeredCheckBox</sender>
<signal>toggled(bool)</signal>
<receiver>nativeCount</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>93</x>
<y>326</y>
</hint>
<hint type="destinationlabel">
<x>150</x>
<y>349</y>
</hint>
</hints>
</connection>
<connection>
<sender>registeredCheckBox</sender>
<signal>toggled(bool)</signal>
<receiver>nativeModifiersSpinBox</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>108</x>
<y>327</y>
</hint>
<hint type="destinationlabel">
<x>111</x>
<y>305</y>
</hint>
</hints>
</connection>
<connection>
<sender>registeredCheckBox</sender>
<signal>toggled(bool)</signal>
<receiver>nativeKeySpinBox</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>183</x>
<y>327</y>
</hint>
<hint type="destinationlabel">
<x>194</x>
<y>279</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
+37
View File
@@ -28,6 +28,12 @@ QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister
this->setShortcut(key, modifiers, autoRegister); this->setShortcut(key, modifiers, autoRegister);
} }
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) :
QHotkey(parent)
{
this->setNativeShortcut(shortcut, autoRegister);
}
QHotkey::~QHotkey() QHotkey::~QHotkey()
{ {
if(this->registered) if(this->registered)
@@ -52,6 +58,11 @@ Qt::KeyboardModifiers QHotkey::modifiers() const
return this->mods; return this->mods;
} }
QHotkey::NativeShortcut QHotkey::currentNativeShortcut() const
{
return this->nativeShortcut;
}
bool QHotkey::isRegistered() const bool QHotkey::isRegistered() const
{ {
return this->registered; return this->registered;
@@ -118,6 +129,32 @@ bool QHotkey::resetShortcut()
return true; 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) bool QHotkey::setRegistered(bool registered)
{ {
if(this->registered && !registered) if(this->registered && !registered)
+7
View File
@@ -42,6 +42,8 @@ public:
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR); explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
//! Constructs a hotkey with a key and modifiers and optionally registers it //! 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); 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 //! Destructor
~QHotkey(); ~QHotkey();
@@ -54,6 +56,9 @@ public:
//! READ-Accessor for QHotkey::shortcut - the modifiers only //! READ-Accessor for QHotkey::shortcut - the modifiers only
Qt::KeyboardModifiers modifiers() const; Qt::KeyboardModifiers modifiers() const;
// Get the current native shortcut
NativeShortcut currentNativeShortcut() const;
public slots: public slots:
//! WRITE-Accessor for QHotkey::registered //! WRITE-Accessor for QHotkey::registered
bool setRegistered(bool registered); bool setRegistered(bool registered);
@@ -65,6 +70,8 @@ public slots:
//! RESET-Accessor for QHotkey::shortcut //! RESET-Accessor for QHotkey::shortcut
bool resetShortcut(); bool resetShortcut();
bool setNativeShortcut(NativeShortcut nativeShortcut, bool autoRegister = false);
signals: signals:
//! Will be emitted if the shortcut is pressed //! Will be emitted if the shortcut is pressed
void activated(QPrivateSignal); void activated(QPrivateSignal);