-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypresshandler.cpp
More file actions
34 lines (30 loc) · 1.11 KB
/
keypresshandler.cpp
File metadata and controls
34 lines (30 loc) · 1.11 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
#include "keypresshandler.h"
#include <QKeyEvent>
#include <QEvent>
// -----------------------------------------------------------------------------
void KeyPressHandler::addKey(int key)
{
pressed_keys_.insert(key);
}
// -----------------------------------------------------------------------------
void KeyPressHandler::removeKey(int key)
{
if(pressed_keys_.find(key) != pressed_keys_.end())
pressed_keys_.erase(pressed_keys_.find(key));
}
// -----------------------------------------------------------------------------
KeyPressHandler::KeyPressHandler() : pressed_keys_(std::set<int>())
{
}
// -----------------------------------------------------------------------------
bool KeyPressHandler::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
{
QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
if(event->type() == QEvent::KeyPress) addKey(key_event->key());
else if(event->type() == QEvent::KeyRelease) removeKey(key_event->key());
emit pressedKeysChanged(pressed_keys_);
}
return QObject::eventFilter(obj, event);
}