From fc969e32d073e1994535cf5b56a51639d59b0a7a Mon Sep 17 00:00:00 2001 From: leexu007 Date: Fri, 22 Jul 2022 16:12:53 +0800 Subject: [PATCH] * Click save button save the hotkey + Setting dialog take fouce * Show hotkey when key is released * Hotkey display order --- .gitignore | 3 +- setting.xml | 2 +- setting/settingdlg.cpp | 96 +++++++++++++++++++++++++++--------------- setting/settingdlg.h | 12 +++--- starterui.cpp | 1 - 5 files changed, 71 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 8ab254c..c75d889 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ ui_*.h .qmake.* *.pro.user *.vcxproj* -logs \ No newline at end of file +logs +setting.xml \ No newline at end of file diff --git a/setting.xml b/setting.xml index 21a516e..db87d59 100644 --- a/setting.xml +++ b/setting.xml @@ -1,5 +1,5 @@ 1 - 1118801 + 1118810 diff --git a/setting/settingdlg.cpp b/setting/settingdlg.cpp index 50d1878..c8dddc8 100644 --- a/setting/settingdlg.cpp +++ b/setting/settingdlg.cpp @@ -9,6 +9,7 @@ #include #include +#include #ifdef _WINDOWS #include @@ -20,6 +21,7 @@ SettingDlg::SettingDlg(QWidget *parent, Qt::WindowFlags f) , m_KeyValue(0) , m_EnableHotKey(0) , m_HotKeyStat(0) + , m_OrigKeyValue(0) { ui.setupUi(this); setWindowFlags(windowFlags() | Qt::Tool); @@ -37,10 +39,16 @@ SettingDlg::~SettingDlg() void SettingDlg::closeEvent(QCloseEvent* event) { - (void*)event; + event->ignore(); hide(); } +void SettingDlg::showEvent(QShowEvent* event) +{ + initStat(); + QDialog::showEvent(event); +} + void SettingDlg::initDlg() { // load qss @@ -80,16 +88,19 @@ void SettingDlg::initStat() int32_t stat = static_cast(GetXMLConfig().GetConfigNum2("config", "enable")); uint32_t value = static_cast(GetXMLConfig().GetConfigNum2("config", "hotkey")); m_EnableHotKey->setChecked(stat != 0); - + emit UpdateHotKeyText(value); - emit UpdateHotKeyValue(value); + + m_KeyValue = value; + + OnUpdateHotKeyValue(); + m_OrigKeyValue = value; } void SettingDlg::setupSingal() { connect(m_EnableHotKey, SIGNAL(stateChanged(int)), this, SLOT(OnUseHotKeyChecked(int))); connect(this, SIGNAL(UpdateHotKeyText(uint32_t)), this, SLOT(OnUpdateHotKeyText(uint32_t))); - connect(this, SIGNAL(UpdateHotKeyValue(uint32_t)), this, SLOT(OnUpdateHotKeyValue(uint32_t))); connect(this, SIGNAL(UpdateHotKeyResult(bool)), this, SLOT(OnUpdateHotKeyResult(bool))); QPushButton* save = findChild("settingSaveBtn"); @@ -163,31 +174,28 @@ void SettingDlg::OnUpdateHotKeyText(uint32_t value) { QString vk_str = getHotKeyStr(value); m_HotKeyValue->setText(vk_str); + + m_KeyValue = value; } -void SettingDlg::OnUpdateHotKeyValue(uint32_t value) +void SettingDlg::OnUpdateHotKeyValue() { #ifdef Q_OS_WIN32 - if (m_KeyValue && m_KeyValue == value) + if (m_OrigKeyValue && m_OrigKeyValue == m_KeyValue) { return; } - if (m_KeyValue) - { - UnregisterHotKey(reinterpret_cast(parentWidget()->winId()), m_KeyValue); - m_KeyValue = 0; - } - if (!value) + if (m_OrigKeyValue) { - return; + UnregisterHotKey(reinterpret_cast(parentWidget()->winId()), m_OrigKeyValue); } UINT fsModifiers, vk; fsModifiers = vk = 0; uint8_t key0, key1, key2; - key0 = ((value >> 16) & 0x000000FF); - key1 = ((value >> 8) & 0x000000FF); - key2 = (value & 0x000000FF); + key0 = ((m_KeyValue >> 16) & 0x000000FF); + key1 = ((m_KeyValue >> 8) & 0x000000FF); + key2 = (m_KeyValue & 0x000000FF); if (key0 == VK_CONTROL || key1 == VK_CONTROL || key2 == VK_CONTROL) { @@ -219,17 +227,9 @@ void SettingDlg::OnUpdateHotKeyValue(uint32_t value) return; } - BOOL result = RegisterHotKey(reinterpret_cast(parentWidget()->winId()), value, fsModifiers, vk); + BOOL result = RegisterHotKey(reinterpret_cast(parentWidget()->winId()), m_KeyValue, fsModifiers, vk); emit UpdateHotKeyResult(result ? true : false); - - if (!result) - { - return; - } - - m_KeyValue = value; - Q_EMIT SaveHotKeyConfig(); #endif // Q_OS_WIN32 } @@ -241,6 +241,10 @@ void SettingDlg::OnUpdateHotKeyResult(bool success) void SettingDlg::OnSaveHotKeyConfig() { + OnUpdateHotKeyValue(); + + m_OrigKeyValue = m_KeyValue; + GetXMLConfig().SetConfigNum2("config", "enable", m_EnableHotKey->isChecked() ? 1 : 0); GetXMLConfig().SetConfigNum2("config", "hotkey", m_KeyValue); GetXMLConfig().SaveConfig(SETTING_XML_NAME); @@ -273,7 +277,12 @@ bool SettingDlg::nativeEvent(const QByteArray& eventType, void* message, long* r break; } - if (key_msg == 1) + if (key == VK_ESCAPE || key == VK_RETURN || key == VK_CAPITAL) + { + return QWidget::nativeEvent(eventType, message, result); + } + + if (key_msg == 1) { for (int i = 0; key && i < sizeof(m_SetKeyValue) / sizeof(m_SetKeyValue[0]); i++) { @@ -288,26 +297,45 @@ bool SettingDlg::nativeEvent(const QByteArray& eventType, void* message, long* r if (!m_SetKeyValue[i]) { m_SetKeyValue[i] = key; - - uint32_t value = ((m_SetKeyValue[0] << 16) & 0x00FF0000) | ((m_SetKeyValue[1] << 8) & 0x0000FF00) | (m_SetKeyValue[2] & 0x000000FF); - emit UpdateHotKeyText(value); break; } } } else if (key_msg == 2) { - for (int i = 0; i < sizeof(m_SetKeyValue) / sizeof(m_SetKeyValue[0]); i++) + int c = 0; + for (; c < sizeof(m_SetKeyValue) / sizeof(m_SetKeyValue[0]); c++) { - if (m_SetKeyValue[i] && (GetKeyState(m_SetKeyValue[i]) & 0x8000)) + if (!m_SetKeyValue[c]) { - return QWidget::nativeEvent(eventType, message, result); + break; + } + } + + if (c <= 1) + { + return QWidget::nativeEvent(eventType, message, result); + } + + for (int i = 0; i < sizeof(m_SetKeyValue) / sizeof(m_SetKeyValue[0]); i++) + { + for (int j = i; j < sizeof(m_SetKeyValue) / sizeof(m_SetKeyValue[0]); j++) + { + if (m_SetKeyValue[i] < m_SetKeyValue[j]) + { + std::swap(m_SetKeyValue[i], m_SetKeyValue[j]); + } } } - uint32_t value = ((m_SetKeyValue[0] << 16) & 0x00FF0000) | ((m_SetKeyValue[1] << 8) & 0x0000FF00) | (m_SetKeyValue[2] & 0x000000FF); - emit UpdateHotKeyValue(value); + if (m_SetKeyValue[1] == VK_CONTROL) { + std::swap(m_SetKeyValue[1], m_SetKeyValue[2]); + } + + uint32_t value = ((m_SetKeyValue[2] << 16) & 0x00FF0000) | ((m_SetKeyValue[1] << 8) & 0x0000FF00) | (m_SetKeyValue[0] & 0x000000FF); emit UpdateHotKeyText(value); + + memset(m_SetKeyValue, 0, sizeof(m_SetKeyValue)); } #endif // _WINDOWS diff --git a/setting/settingdlg.h b/setting/settingdlg.h index 9fbbc17..a26e294 100644 --- a/setting/settingdlg.h +++ b/setting/settingdlg.h @@ -23,7 +23,8 @@ class SettingDlg : public QDialog { virtual ~SettingDlg(); protected: - void closeEvent(QCloseEvent*) override; + void closeEvent(QCloseEvent*) override; + void showEvent(QShowEvent*) override; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) virtual bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result); #else @@ -39,19 +40,18 @@ class SettingDlg : public QDialog { signals: void SaveHotKeyConfig(); void UpdateHotKeyText(uint32_t value); - void UpdateHotKeyValue(uint32_t value); void UpdateHotKeyResult(bool success); private slots: void OnUseHotKeyChecked(int stat); - void OnUpdateHotKeyText(uint32_t value); // 更新热键显示 - void OnUpdateHotKeyValue(uint32_t value); // 注册热键 - void OnUpdateHotKeyResult(bool success); // 热键注册结果 + void OnUpdateHotKeyText(uint32_t value); // 更新热键显示 + void OnUpdateHotKeyValue(); // 注册热键 + void OnUpdateHotKeyResult(bool success); // 热键注册结果 void OnSaveHotKeyConfig(); private: Ui::SettingDlg ui; - uint32_t m_KeyValue; + uint32_t m_KeyValue, m_OrigKeyValue; uint32_t m_SetKeyValue[3]; QCheckBox* m_EnableHotKey; QPushButton* m_HotKeyStat; diff --git a/starterui.cpp b/starterui.cpp index 47d7293..df216b6 100644 --- a/starterui.cpp +++ b/starterui.cpp @@ -35,7 +35,6 @@ StarterUI::StarterUI() #endif // IS_TEST_VER connect(this, SIGNAL(SatrtShot()), this, SLOT(OnStartShot())); - connect(this, SIGNAL(CheckHotKey(uint32_t)), &m_SettingDlg, SIGNAL(UpdateHotKeyValue(uint32_t))); // connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated); trayIcon->show();