This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshortcutedit.cpp
53 lines (46 loc) · 1.73 KB
/
shortcutedit.cpp
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
#include "stdafx.h"
#include "shortcutedit.h"
ShortcutEdit::ShortcutEdit(QWidget *parent) : QLineEdit(parent) {}
bool ShortcutEdit::event(QEvent *e)
{
if (e->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
int keyInt = keyEvent->key();
Qt::Key key = static_cast<Qt::Key>(keyInt);
if (key == Qt::Key_unknown)
{
qDebug() << "Unknown key from a macro probably";
return false;
}
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if (key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Alt || key == Qt::Key_Meta)
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta";
return false;
}
// check for a combination of user clicks
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText = keyEvent->text();
// if the keyText is empty than it's a special key like F1, F5, ...
qDebug() << "Pressed Key:" << keyText;
std::map<Qt::KeyboardModifier, Qt::Modifier> m = {
{Qt::ShiftModifier, Qt::SHIFT},
{Qt::ControlModifier, Qt::CTRL},
{Qt::AltModifier, Qt::ALT},
{Qt::MetaModifier, Qt::META},
};
for (auto k : m)
{
if (modifiers & k.first)
keyInt += k.second;
}
QString shortcut = text();
if (shortcut.contains(", ") || shortcut.isEmpty())
setText(QKeySequence(keyInt).toString());
else
setText(shortcut % ", " % QKeySequence(keyInt).toString());
return true;
}
return QLineEdit::event(e);
}