Skip to content

Commit

Permalink
* Click save button save the hotkey
Browse files Browse the repository at this point in the history
+ Setting dialog take fouce
* Show hotkey when key is released
* Hotkey display order
  • Loading branch information
leexu007 committed Jul 22, 2022
1 parent c7a7072 commit fc969e3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ ui_*.h
.qmake.*
*.pro.user
*.vcxproj*
logs
logs
setting.xml
2 changes: 1 addition & 1 deletion setting.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<config>
<enable>1</enable>
<hotkey>1118801</hotkey>
<hotkey>1118810</hotkey>
</config>

96 changes: 62 additions & 34 deletions setting/settingdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <QFile>
#include <QVariant>
#include <QCloseEvent>

#ifdef _WINDOWS
#include <Windows.h>
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -80,16 +88,19 @@ void SettingDlg::initStat()
int32_t stat = static_cast<int32_t>(GetXMLConfig().GetConfigNum2("config", "enable"));
uint32_t value = static_cast<uint32_t>(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<QPushButton*>("settingSaveBtn");
Expand Down Expand Up @@ -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<HWND>(parentWidget()->winId()), m_KeyValue);
m_KeyValue = 0;
}
if (!value)
if (m_OrigKeyValue)
{
return;
UnregisterHotKey(reinterpret_cast<HWND>(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)
{
Expand Down Expand Up @@ -219,17 +227,9 @@ void SettingDlg::OnUpdateHotKeyValue(uint32_t value)
return;
}

BOOL result = RegisterHotKey(reinterpret_cast<HWND>(parentWidget()->winId()), value, fsModifiers, vk);
BOOL result = RegisterHotKey(reinterpret_cast<HWND>(parentWidget()->winId()), m_KeyValue, fsModifiers, vk);

emit UpdateHotKeyResult(result ? true : false);

if (!result)
{
return;
}

m_KeyValue = value;
Q_EMIT SaveHotKeyConfig();
#endif // Q_OS_WIN32
}

Expand All @@ -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);
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions setting/settingdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion starterui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fc969e3

Please sign in to comment.