-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyMap.h
108 lines (84 loc) · 3.09 KB
/
KeyMap.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef MIDI2KB_KEYMAP_H
#define MIDI2KB_KEYMAP_H
#include <string>
#include "KeyEvent.h"
enum class KeyMapCondition {
ThresholdExact, ThresholdUp, ThresholdDown, ThresholdAny,
Any = 0x70,
Press = 0x90,
Release = 0x80,
Pressure = 0xA0,
PitchBend = 0xE0,
ProgramChange = 0xC0,
ChannelPressure = 0xD0,
ControllerChange = 0xB0,
};
enum class KeyMapAction {
Hold, Press, Toggle, Release
};
struct KeyMap {
unsigned char key;
std::string toKey;
KeyMapCondition condition, threshold;
unsigned short trigger;
KeyMapAction action;
bool toggle = false;
KeyMap() : key{0}, toKey{""}, condition{0}, threshold{0}, trigger{0} {}
bool CheckEvent(KeyEvent& event)
{
if(condition != KeyMapCondition::Any)
if(condition != (KeyMapCondition ) event.type) return false;
switch(event.type)
{
case KeyEventType::PitchBend:
case KeyEventType::ProgramChange:
case KeyEventType::ChannelPressure:
case KeyEventType::ControllerChange:
return false;
default:
{
if(key != event.byte1) return false;
if(threshold == KeyMapCondition::ThresholdAny)
return true;
if(threshold == KeyMapCondition::ThresholdExact)
return trigger == event.byte2;
if(threshold == KeyMapCondition::ThresholdUp)
return event.byte2 >= trigger;
if(threshold == KeyMapCondition::ThresholdDown)
return event.byte2 <= trigger;
}
}
return false;
}
std::string ToString()
{
std::stringstream text;
std::string condition_, threshold_, action_;
unsigned short key_ = key;
switch (condition)
{
case KeyMapCondition::Any: condition_ = "key emits event"; break;
case KeyMapCondition::Press: condition_ = "key is pressed"; break;
case KeyMapCondition::Release: condition_ = "key is released"; break;
case KeyMapCondition::Pressure: condition_ = "key pressure changes"; break;
}
switch (threshold)
{
case KeyMapCondition::ThresholdUp: threshold_ = ">="; break;
case KeyMapCondition::ThresholdDown: threshold_ = "<="; break;
case KeyMapCondition::ThresholdAny:
case KeyMapCondition::ThresholdExact: threshold_ = "="; break;
}
switch(action)
{
case KeyMapAction::Hold: action_ = "hold"; break;
case KeyMapAction::Press: action_ = "press"; break;
case KeyMapAction::Toggle: action_ = "toggle"; break;
case KeyMapAction::Release: action_ = "release"; break;
}
text << key_ << " -> " << action_ << ' ' << toKey << " if " << condition_ << " and value " << threshold_ << ' ' <<
(threshold != KeyMapCondition::ThresholdAny ? std::to_string(trigger) : "any value");
return text.str();
}
};
#endif //MIDI2KB_KEYMAP_H