Repository navigation
Expand file tree
/
Copy pathuioeventloop.cpp
More file actions
61 lines (52 loc) · 1.8 KB
/
Copy pathuioeventloop.cpp
File metadata and controls
61 lines (52 loc) · 1.8 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
#include "uioeventloop.h"
#include "uiokey.h"
#include <QDebug>
#include <thread>
static UioEventLoop* g_instance = nullptr;
UioEventLoop::UioEventLoop(QObject *parent)
: QObject(parent) {
g_instance = this;
hook_set_dispatch_proc(&UioEventLoop::dispatch_proc);
// Start uiohook in a separate thread
std::thread([]() {
if (hook_run() != UIOHOOK_SUCCESS) {
qWarning() << "Failed to start uiohook";
}
}).detach();
}
UioEventLoop::~UioEventLoop() {
hook_stop(); // stop hook on destruction
g_instance = nullptr;
}
void UioEventLoop::dispatch_proc(uiohook_event * const event) {
if (g_instance) {
g_instance->handleEvent(event);
}
}
void UioEventLoop::handleEvent(uiohook_event * const event) {
auto keycode = event->data.keyboard.keycode;
auto rawcode = event->data.keyboard.rawcode;
if (event->type == EVENT_KEY_PRESSED) {
//qDebug() << "rawcode:" << rawcode << " keycode:" << keycode;
if (IsKeycodeModifierKey(event->data.keyboard.keycode, event->data.keyboard.rawcode)) {
#ifdef Q_OS_LINUX
keycode = translateLinuxRawcodeToKeycode(rawcode);
#endif
emit modifierPressed(keycode, rawcode);
emit keyPressed(keycode, rawcode); // send as normal key too
} else {
emit keyPressed(keycode, rawcode);
}
}
else if (event->type == EVENT_KEY_RELEASED) {
if (IsKeycodeModifierKey(event->data.keyboard.keycode, event->data.keyboard.rawcode)) {
#ifdef Q_OS_LINUX
keycode = translateLinuxRawcodeToKeycode(rawcode);
#endif
emit modifierReleased(keycode, rawcode);
emit keyReleased(keycode, rawcode);
} else {
emit keyReleased(keycode, rawcode);
}
}
}