forked from BearWare/TeamTalk5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeycompdlg.cpp
More file actions
248 lines (207 loc) · 7.21 KB
/
keycompdlg.cpp
File metadata and controls
248 lines (207 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (C) 2023, Bjørn D. Rasmussen, BearWare.dk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "keycompdlg.h"
#include "utilhotkey.h"
#include "appinfo.h"
#include <QDebug>
#include <QKeySequence>
#include <QVector>
#include <QMessageBox>
#include <QKeyEvent>
#if defined(Q_OS_DARWIN)
#include <Carbon/Carbon.h>
#endif
extern TTInstance* ttInst;
KeyCompDlg::KeyCompDlg(HotKeyID hkID, QWidget * parent/* = 0*/)
: QDialog(parent, QT_DEFAULT_DIALOG_HINTS)
{
ui.setupUi(this);
setWindowIcon(QIcon(APPICON));
ui.groupBox->setTitle(tr("Setup Hotkey: %1").arg(getHotKeyName(hkID)));
#ifdef Q_OS_WIN32
HWND hWnd = reinterpret_cast<HWND>(this->winId());
TT_HotKey_InstallTestHook(ttInst, hWnd, WM_TEAMTALK_HOTKEYEVENT);
#endif
}
KeyCompDlg::~KeyCompDlg()
{
#if defined(Q_OS_WIN32)
TT_HotKey_RemoveTestHook(ttInst);
#elif defined(Q_OS_DARWIN)
if(m_hotkey.empty())
QMessageBox::warning(this, tr("Key Combination"),
tr("Modifiers (Option, Control, Command and Shift) must be used in combination with other keys."));
#endif
}
#if defined(Q_OS_WIN32)
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
bool KeyCompDlg::nativeEvent(const QByteArray& eventType, void* message,
long* result)
#else
bool KeyCompDlg::nativeEvent(const QByteArray& eventType, void* message,
qintptr* result)
#endif
{
MSG* msg = reinterpret_cast<MSG*>(message);
if(msg->message == WM_TEAMTALK_HOTKEYEVENT)
{
if(msg->lParam)
{
m_hotkey.push_back(msg->wParam);
ui.keycompEdit->setText(getHotKeyText(m_hotkey));
m_activekeys.insert(msg->wParam);
}
else
{
// if KeyCompDlg is opened from a key press (e.g. Space-key) then
// we receive an unwanted WM_TEAMTALK_HOTKEYEVENT
if (!m_activekeys.contains(msg->wParam))
return QDialog::nativeEvent(eventType, message, result);
m_activekeys.remove(msg->wParam);
}
if(m_activekeys.size() == 0)
this->accept();
}
return QDialog::nativeEvent(eventType, message, result);
}
#elif defined(Q_OS_LINUX)
void KeyCompDlg::keyPressEvent(QKeyEvent* event)
{
m_hotkey.clear();
Qt::KeyboardModifiers mods = event->modifiers();
if(mods & Qt::CTRL)
m_activekeys.insert(Qt::CTRL);
if(mods & Qt::ALT)
m_activekeys.insert(Qt::ALT);
if(mods & Qt::SHIFT)
m_activekeys.insert(Qt::SHIFT);
if(mods & Qt::META)
m_activekeys.insert(Qt::META);
switch(event->key())
{
case Qt::Key_Control :
case Qt::Key_Alt :
case Qt::Key_Shift :
case Qt::Key_Meta :
break;
default:
m_activekeys.insert(event->key());
}
QSet<INT32>::const_iterator ite = m_activekeys.begin();
for(;ite != m_activekeys.end();ite++)
m_hotkey.push_back(*ite);
ui.keycompEdit->setText(getHotKeyText(m_hotkey));
}
void KeyCompDlg::keyReleaseEvent(QKeyEvent* event)
{
// if KeyCompDlg is opened from a key press (e.g. Space-key) then
// we receive an unwanted keyReleaseEvent()
if (!m_activekeys.contains(event->key()))
{
QDialog::keyReleaseEvent(event);
return;
}
if(event->isAutoRepeat())
return;
if(event->modifiers() == 0)
this->accept();
}
#elif defined(Q_OS_DARWIN)
void KeyCompDlg::keyPressEvent(QKeyEvent* event)
{
qDebug() << "KeyCompDlg::keyPressEvent";
qDebug() << "Native: " << QString("%1").arg(event->nativeModifiers(), 0, 16)
<< "modifiers: " << QString("%1").arg(int(event->modifiers()), 0, 16)
<< "key: " << QString("%1").arg(event->key(), 0, 16);
if (m_hotkey.empty())
{
for(int i=0;i<MAC_HOTKEY_SIZE;i++)
m_hotkey.push_back(MAC_NO_KEY);
}
switch(event->key())
{
case 0 : break;
case Qt::Key_Control :
m_hotkey[0] = m_hotkey[0] == (INT32)MAC_NO_KEY? 0 : m_hotkey[0];
m_hotkey[0] |= cmdKey;
qDebug() << "Ctrl";
break;
case Qt::Key_Alt :
m_hotkey[0] = m_hotkey[0] == (INT32)MAC_NO_KEY? 0 : m_hotkey[0];
m_hotkey[0] |= optionKey;
qDebug() << "Alt";
break;
case Qt::Key_Shift :
m_hotkey[0] = m_hotkey[0] == (INT32)MAC_NO_KEY? 0 : m_hotkey[0];
m_hotkey[0] |= shiftKey;
qDebug() << "Shift";
break;
case Qt::Key_Meta :
m_hotkey[0] = m_hotkey[0] == (INT32)MAC_NO_KEY? 0 : m_hotkey[0];
m_hotkey[0] |= controlKey;
qDebug() << "MEta";
break;
default:
m_hotkey[1] = event->nativeVirtualKey();
qDebug() << "VK " << event->nativeVirtualKey();
break;
}
m_activekeys.insert(event->key());
Q_ASSERT(m_hotkey.size() == 2);
ui.keycompEdit->setText(getHotKeyText(m_hotkey));
}
void KeyCompDlg::keyReleaseEvent(QKeyEvent* event)
{
qDebug() << "KeyCompDlg::keyReleaseEvent";
qDebug() << "Native: " << QString("%1").arg(event->nativeModifiers(), 0, 16)
<< "modifiers: " << QString("%1").arg(int(event->modifiers()), 0, 16)
<< "key: " << QString("%1").arg(event->key(), 0, 16)
<< "active keys count " << m_activekeys.size();
// if KeyCompDlg is opened from a key press (e.g. Space-key) then
// we receive an unwanted keyReleaseEvent()
if (!m_activekeys.contains(event->key()))
{
QDialog::keyReleaseEvent(event);
return;
}
if(event->isAutoRepeat())
return;
m_activekeys.remove(event->key());
bool close_dlg = false;
if(m_activekeys.isEmpty())
close_dlg = true;
//when holding cmdKey and pressing another key hides the other
//key-release event, so we just have to close the dialog
switch(event->key())
{
case Qt::Key_Control :
close_dlg = true;
qDebug() << "Closing dialog due to cmdKey release";
break;
}
if(close_dlg && m_hotkey[0] != (INT32)MAC_NO_KEY && m_hotkey[1] == (INT32)MAC_NO_KEY)
{
QMessageBox::information(this, tr("Invalid key combination"),
tr("macOS does not support only modifier keys, "
"i.e. Cmd, Option and Shift must be used in "
"combination with other non-modifier keys."));
m_hotkey[0] = m_hotkey[1] = MAC_NO_KEY;
}
if(close_dlg)
this->accept();
}
#endif