forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifierKeyListener.h
More file actions
176 lines (155 loc) · 5.12 KB
/
ModifierKeyListener.h
File metadata and controls
176 lines (155 loc) · 5.12 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include "Config.h"
/*
// Copyright (c) 2023 Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
// A class that does more reliable modifier listening than JUCE has by default
// Create one broadcaster class and attach listeners to that
struct ModifierKeyListener {
virtual void shiftKeyChanged(bool isHeld) { ignoreUnused(isHeld); }
virtual void commandKeyChanged(bool isHeld) { ignoreUnused(isHeld); }
virtual void altKeyChanged(bool isHeld) { ignoreUnused(isHeld); }
virtual void ctrlKeyChanged(bool isHeld) { ignoreUnused(isHeld); }
virtual void spaceKeyChanged(bool isHeld) { ignoreUnused(isHeld); }
virtual void middleMouseChanged(bool isHeld) { ignoreUnused(isHeld); }
JUCE_DECLARE_WEAK_REFERENCEABLE(ModifierKeyListener)
};
class ModifierKeyBroadcaster {
public:
ModifierKeyBroadcaster()
{
}
void addModifierKeyListener(ModifierKeyListener* listener)
{
listeners.add(listener);
}
void removeModifierKeyListener(ModifierKeyListener* listener)
{
listeners.remove_all(listener);
}
void setModifierKeys(ModifierKeys const& mods)
{
// Handle mod down
if (mods.isShiftDown() && !shiftWasDown) {
callListeners(Shift, true);
shiftWasDown = true;
}
if (mods.isCommandDown() && !commandWasDown) {
callListeners(Command, true);
commandWasDown = true;
}
if (mods.isAltDown() && !altWasDown) {
callListeners(Alt, true);
altWasDown = true;
}
if (mods.isCtrlDown() && !ctrlWasDown) {
callListeners(Ctrl, true);
ctrlWasDown = true;
}
if (mods.isMiddleButtonDown() && !middleMouseWasDown) {
callListeners(MiddleMouse, true);
middleMouseWasDown = true;
}
if (KeyPress::isKeyCurrentlyDown(KeyPress::spaceKey) && !spaceWasDown) {
callListeners(Space, true);
spaceWasDown = true;
}
// Handle mod up
if (!mods.isShiftDown() && shiftWasDown) {
callListeners(Shift, false);
shiftWasDown = false;
}
if (!mods.isCommandDown() && commandWasDown) {
callListeners(Command, false);
commandWasDown = false;
}
if (!mods.isAltDown() && altWasDown) {
callListeners(Alt, false);
altWasDown = false;
}
if (!mods.isCtrlDown() && ctrlWasDown) {
callListeners(Ctrl, false);
ctrlWasDown = false;
}
if (!mods.isMiddleButtonDown() && middleMouseWasDown) {
callListeners(MiddleMouse, false);
middleMouseWasDown = false;
}
if (!KeyPress::isKeyCurrentlyDown(KeyPress::spaceKey) && spaceWasDown) {
callListeners(Space, false);
spaceWasDown = false;
}
}
private:
enum Modifier {
Shift,
Command,
Alt,
Ctrl,
Space,
MiddleMouse
};
void callListeners(Modifier const mod, bool const down)
{
for (auto const& listener : listeners) {
if (!listener)
continue;
switch (mod) {
case Shift: {
listener->shiftKeyChanged(down);
break;
}
case Command: {
listener->commandKeyChanged(down);
break;
}
case Alt: {
listener->altKeyChanged(down);
break;
}
case Ctrl: {
listener->ctrlKeyChanged(down);
break;
}
case Space: {
listener->spaceKeyChanged(down);
break;
}
case MiddleMouse: {
listener->middleMouseChanged(down);
break;
}
}
}
}
virtual bool isActiveWindow() { return true; }
bool shiftWasDown = false;
bool commandWasDown = false;
bool altWasDown = false;
bool ctrlWasDown = false;
bool spaceWasDown = false;
bool middleMouseWasDown = false;
class ModifierKeyTimer : public Timer
{
public:
ModifierKeyTimer(ModifierKeyBroadcaster& parent) : p(parent)
{
startTimer(50);
}
void timerCallback() override
{
// If a window that's not coming from our app is top-level, ignore
// key commands
if (ProjectInfo::isStandalone && !p.isActiveWindow()) {
return;
}
auto const mods = ModifierKeys::getCurrentModifiersRealtime();
p.setModifierKeys(mods);
}
ModifierKeyBroadcaster& p;
};
ModifierKeyTimer timer = ModifierKeyTimer(*this);
HeapArray<WeakReference<ModifierKeyListener>> listeners;
};