-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshortcuts.cpp
88 lines (68 loc) · 2.58 KB
/
shortcuts.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
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
#include <QDebug>
#include <QKeySequence>
#include <QMetaMethod>
#include <QMetaObject>
#include <QShortcut>
#include <QKeyEvent>
#include <QLineEdit>
#include <QGraphicsScene>
#include "shortcuts.h"
Shortcuts::Shortcuts(QObject *parent)
: QObject(parent)
{
qApp->installEventFilter(this);
add(QKeySequence::Quit, QCoreApplication::instance(), "quit");
add(QKeySequence(Qt::Key_Space), Player::instance(), "togglePlayPause");
add(QKeySequence(Qt::Key_Right), Player::instance(), "skipForward");
add(QKeySequence(Qt::Key_Left), Player::instance(), "skipBackward");
add(QKeySequence(Qt::Key_Up), Player::instance(), "volumeUp");
add(QKeySequence(Qt::Key_Down), Player::instance(), "volumeDown");
}
Shortcuts *Shortcuts::instance()
{
static Shortcuts* instance = new Shortcuts;
return instance;
}
QObject *Shortcuts::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return Shortcuts::instance(); // C++ and QML instance
}
bool Shortcuts::eventFilter(QObject *obj, QEvent *e)
{
// https://stackoverflow.com/questions/12192780/assigning-keyboard-shortcuts-to-qml-components
if(e->type() == QEvent::KeyRelease) {
// key release
mAlreadySent = false;
return QObject::eventFilter(obj, e);
}
if (e->type() != QEvent::KeyPress)
return QObject::eventFilter(obj, e);
if ((dynamic_cast<QGraphicsScene*>(obj)) || (obj->objectName() == "blockShortcut") || (dynamic_cast<QLineEdit*>(obj)) )
return QObject::eventFilter(obj, e);
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);
// Just mod keys is not enough for a shortcut, block them just by returning.
if (keyEvent->key() >= Qt::Key_Shift && keyEvent->key() <= Qt::Key_Alt)
return QObject::eventFilter(obj, e);
int keyInt = keyEvent->modifiers() + keyEvent->key();
for (ShortcutData &shortcut : mShortcuts) {
if (!mAlreadySent && QKeySequence(keyInt) == shortcut.KeySequence) {
mAlreadySent = true;
// execute!
QMetaObject::invokeMethod(shortcut.Context, shortcut.Slot, Qt::DirectConnection);
//QMetaObject::invokeMethod(shortcut.Context, shortcut.Slot, Qt::DirectConnection, Q_ARG(QString, "1234"));
break;
}
}
return QObject::eventFilter(obj, e);
}
void Shortcuts::add(QKeySequence xKeySequence, QObject *xContext, const char* xSlot)
{
// need to remove the first char from slot
ShortcutData s;
s.KeySequence = xKeySequence;
s.Context = xContext;
s.Slot = xSlot;
mShortcuts.append(s);
}