summaryrefslogtreecommitdiffhomepage
path: root/gui/keyboard.h
blob: 07f326ea4697cd2cbde991e905c55d0c8026657d (plain)
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
#pragma once
#include "ui_keyboard_listener.h"
#ifdef _WIN32
#include "opentrack-logic/win32-shortcuts.h"
#include "opentrack-logic/keybinding-worker.hpp"
#endif
#include <QLabel>
#include <QKeyEvent>
#include <QDebug>

class KeyboardListener : public QLabel
{
    Q_OBJECT
    Ui_keyboard_listener ui;
#ifdef _WIN32
    KeybindingWorker::Token token;
#endif
public:
    KeyboardListener(QWidget* parent = nullptr) : QLabel(parent)
#ifdef _WIN32
      , token([&](const Key& k) {
        if(k.guid != "")
        {
            int mods = 0;
            if (k.alt) mods |= Qt::AltModifier;
            if (k.shift) mods |= Qt::ShiftModifier;
            if (k.ctrl) mods |= Qt::ControlModifier;
            joystick_button_pressed(k.guid, k.keycode | mods, k.held);
        }
        else
        {
            Qt::KeyboardModifiers m;
            QKeySequence k_;
            if (win_key::to_qt(k, k_, m))
                key_pressed(static_cast<QVariant>(k_).toInt() | m);
        }
    })
#endif
    {
        ui.setupUi(this);
        setFocusPolicy(Qt::StrongFocus);
    }
#ifndef _WIN32
    void keyPressEvent(QKeyEvent* event) override
    {
        //qDebug() << "k" << (event->key() | event->modifiers());
        switch (event->key() | event->modifiers())
        {
        default:
            emit key_pressed(QKeySequence(event->key() | event->modifiers()));
            break;
        }
    }
#endif
signals:
    void key_pressed(QKeySequence k);
    void joystick_button_pressed(QString guid, int idx, bool held);
};