LMMS
Loading...
Searching...
No Matches
juce_KeyPress.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 : keyCode (code), mods (m), textCharacter (textChar)
31{
32}
33
37
38bool KeyPress::operator== (int otherKeyCode) const noexcept
39{
40 return keyCode == otherKeyCode && ! mods.isAnyModifierKeyDown();
41}
42
43bool KeyPress::operator== (const KeyPress& other) const noexcept
44{
45 return mods.getRawFlags() == other.mods.getRawFlags()
46 && (textCharacter == other.textCharacter
47 || textCharacter == 0
48 || other.textCharacter == 0)
49 && (keyCode == other.keyCode
50 || (keyCode < 256
51 && other.keyCode < 256
53 == CharacterFunctions::toLowerCase ((juce_wchar) other.keyCode)));
54}
55
56bool KeyPress::operator!= (const KeyPress& other) const noexcept { return ! operator== (other); }
57bool KeyPress::operator!= (int otherKeyCode) const noexcept { return ! operator== (otherKeyCode); }
58
65
66//==============================================================================
68{
70 {
71 const char* name;
72 int code;
73 };
74
76 {
77 { "spacebar", KeyPress::spaceKey },
78 { "return", KeyPress::returnKey },
79 { "escape", KeyPress::escapeKey },
80 { "backspace", KeyPress::backspaceKey },
81 { "cursor left", KeyPress::leftKey },
82 { "cursor right", KeyPress::rightKey },
83 { "cursor up", KeyPress::upKey },
84 { "cursor down", KeyPress::downKey },
85 { "page up", KeyPress::pageUpKey },
86 { "page down", KeyPress::pageDownKey },
87 { "home", KeyPress::homeKey },
88 { "end", KeyPress::endKey },
89 { "delete", KeyPress::deleteKey },
90 { "insert", KeyPress::insertKey },
91 { "tab", KeyPress::tabKey },
92 { "play", KeyPress::playKey },
93 { "stop", KeyPress::stopKey },
94 { "fast forward", KeyPress::fastForwardKey },
95 { "rewind", KeyPress::rewindKey }
96 };
97
99 {
100 const char* name;
101 int flag;
102 };
103
105 {
106 { "ctrl", ModifierKeys::ctrlModifier },
107 { "control", ModifierKeys::ctrlModifier },
109 { "shift", ModifierKeys::shiftModifier },
110 { "shft", ModifierKeys::shiftModifier },
111 { "alt", ModifierKeys::altModifier },
112 { "option", ModifierKeys::altModifier },
113 { "command", ModifierKeys::commandModifier },
115 };
116
117 static const char* numberPadPrefix() noexcept { return "numpad "; }
118
119 static int getNumpadKeyCode (const String& desc)
120 {
122 {
123 auto lastChar = desc.trimEnd().getLastCharacter();
124
125 switch (lastChar)
126 {
127 case '0': case '1': case '2': case '3': case '4':
128 case '5': case '6': case '7': case '8': case '9':
129 return (int) (KeyPress::numberPad0 + (int) lastChar - '0');
130
131 case '+': return KeyPress::numberPadAdd;
132 case '-': return KeyPress::numberPadSubtract;
133 case '*': return KeyPress::numberPadMultiply;
134 case '/': return KeyPress::numberPadDivide;
135 case '.': return KeyPress::numberPadDecimalPoint;
136 case '=': return KeyPress::numberPadEquals;
137
138 default: break;
139 }
140
141 if (desc.endsWith ("separator")) return KeyPress::numberPadSeparator;
142 if (desc.endsWith ("delete")) return KeyPress::numberPadDelete;
143 }
144
145 return 0;
146 }
147
148 #if JUCE_MAC || JUCE_IOS
149 struct OSXSymbolReplacement
150 {
151 const char* text;
152 juce_wchar symbol;
153 };
154
155 const OSXSymbolReplacement osxSymbols[] =
156 {
157 { "shift + ", 0x21e7 },
158 { "command + ", 0x2318 },
159 { "option + ", 0x2325 },
160 { "ctrl + ", 0x2303 },
161 { "return", 0x21b5 },
162 { "cursor left", 0x2190 },
163 { "cursor right", 0x2192 },
164 { "cursor up", 0x2191 },
165 { "cursor down", 0x2193 },
166 { "backspace", 0x232b },
167 { "delete", 0x2326 },
168 { "spacebar", 0x2423 }
169 };
170 #endif
171}
172
173//==============================================================================
175{
176 int modifiers = 0;
177
180 modifiers |= KeyPressHelpers::modifierNames[i].flag;
181
182 int key = 0;
183
185 {
187 {
189 break;
190 }
191 }
192
193 if (key == 0)
195
196 if (key == 0)
197 {
198 // see if it's a function key..
199 if (! desc.containsChar ('#')) // avoid mistaking hex-codes like "#f1"
200 {
201 for (int i = 1; i <= 35; ++i)
202 {
203 if (desc.containsWholeWordIgnoreCase ("f" + String (i)))
204 {
205 if (i <= 16) key = F1Key + i - 1;
206 else if (i <= 24) key = F17Key + i - 17;
207 else if (i <= 35) key = F25Key + i - 25;
208 }
209 }
210 }
211
212 if (key == 0)
213 {
214 // give up and use the hex code..
215 auto hexCode = desc.fromFirstOccurrenceOf ("#", false, false)
216 .retainCharacters ("0123456789abcdefABCDEF")
217 .getHexValue32();
218
219 if (hexCode > 0)
220 key = hexCode;
221 else
223 }
224 }
225
226 return KeyPress (key, ModifierKeys (modifiers), 0);
227}
228
230{
231 String desc;
232
233 if (keyCode > 0)
234 {
235 // some keyboard layouts use a shift-key to get the slash, but in those cases, we
236 // want to store it as being a slash, not shift+whatever.
237 if (textCharacter == '/' && keyCode != numberPadDivide)
238 return "/";
239
240 if (mods.isCtrlDown()) desc << "ctrl + ";
241 if (mods.isShiftDown()) desc << "shift + ";
242
243 #if JUCE_MAC || JUCE_IOS
244 if (mods.isAltDown()) desc << "option + ";
245 if (mods.isCommandDown()) desc << "command + ";
246 #else
247 if (mods.isAltDown()) desc << "alt + ";
248 #endif
249
252 return desc + KeyPressHelpers::translations[i].name;
253
254 // not all F keys have consecutive key codes on all platforms
255 if (keyCode >= F1Key && keyCode <= F16Key) desc << 'F' << (1 + keyCode - F1Key);
256 else if (keyCode >= F17Key && keyCode <= F24Key) desc << 'F' << (17 + keyCode - F17Key);
257 else if (keyCode >= F25Key && keyCode <= F35Key) desc << 'F' << (25 + keyCode - F25Key);
259 else if (keyCode >= 33 && keyCode < 176) desc += CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
260 else if (keyCode == numberPadAdd) desc << KeyPressHelpers::numberPadPrefix() << '+';
261 else if (keyCode == numberPadSubtract) desc << KeyPressHelpers::numberPadPrefix() << '-';
262 else if (keyCode == numberPadMultiply) desc << KeyPressHelpers::numberPadPrefix() << '*';
263 else if (keyCode == numberPadDivide) desc << KeyPressHelpers::numberPadPrefix() << '/';
264 else if (keyCode == numberPadSeparator) desc << KeyPressHelpers::numberPadPrefix() << "separator";
266 else if (keyCode == numberPadEquals) desc << KeyPressHelpers::numberPadPrefix() << '=';
267 else if (keyCode == numberPadDelete) desc << KeyPressHelpers::numberPadPrefix() << "delete";
268 else desc << '#' << String::toHexString (keyCode);
269 }
270
271 return desc;
272}
273
275{
276 #if JUCE_MAC || JUCE_IOS
277 auto s = getTextDescription();
278
279 for (int i = 0; i < numElementsInArray (KeyPressHelpers::osxSymbols); ++i)
280 s = s.replace (KeyPressHelpers::osxSymbols[i].text,
281 String::charToString (KeyPressHelpers::osxSymbols[i].symbol));
282
283 return s;
284 #else
285 return getTextDescription();
286 #endif
287}
288
289} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static String toHexString(int number)
Definition String.cpp:1830
static juce_wchar toLowerCase(juce_wchar character) noexcept
Definition juce_CharacterFunctions.cpp:33
static juce_wchar toUpperCase(juce_wchar character) noexcept
Definition juce_CharacterFunctions.cpp:28
static const int numberPadMultiply
Definition juce_KeyPress.h:256
String getTextDescriptionWithIcons() const
Definition juce_KeyPress.cpp:274
static const int playKey
Definition juce_KeyPress.h:263
static const int F35Key
Definition juce_KeyPress.h:241
static const int homeKey
Definition juce_KeyPress.h:204
static const int F24Key
Definition juce_KeyPress.h:230
static const int numberPadEquals
Definition juce_KeyPress.h:260
static const int upKey
Definition juce_KeyPress.h:198
KeyPress()=default
static const int F16Key
Definition juce_KeyPress.h:222
static const int fastForwardKey
Definition juce_KeyPress.h:265
static const int stopKey
Definition juce_KeyPress.h:264
static const int numberPadSeparator
Definition juce_KeyPress.h:258
static bool isKeyCurrentlyDown(int keyCode)
Definition juce_linux_Windowing.cpp:804
static const int endKey
Definition juce_KeyPress.h:205
static const int F1Key
Definition juce_KeyPress.h:207
static const int tabKey
Definition juce_KeyPress.h:192
static const int numberPad0
Definition juce_KeyPress.h:243
static const int numberPad9
Definition juce_KeyPress.h:252
static const int numberPadAdd
Definition juce_KeyPress.h:254
static const int rightKey
Definition juce_KeyPress.h:201
static const int F17Key
Definition juce_KeyPress.h:223
static const int rewindKey
Definition juce_KeyPress.h:266
ModifierKeys mods
Definition juce_KeyPress.h:271
static const int deleteKey
Definition juce_KeyPress.h:194
static const int insertKey
Definition juce_KeyPress.h:196
int keyCode
Definition juce_KeyPress.h:270
static const int F25Key
Definition juce_KeyPress.h:231
bool isCurrentlyDown() const
Definition juce_KeyPress.cpp:59
static const int downKey
Definition juce_KeyPress.h:199
static const int numberPadDelete
Definition juce_KeyPress.h:261
static KeyPress createFromDescription(const String &textVersion)
Definition juce_KeyPress.cpp:174
static const int spaceKey
Definition juce_KeyPress.h:189
static const int escapeKey
Definition juce_KeyPress.h:190
static const int numberPadDivide
Definition juce_KeyPress.h:257
static const int returnKey
Definition juce_KeyPress.h:191
static const int leftKey
Definition juce_KeyPress.h:200
static const int pageUpKey
Definition juce_KeyPress.h:202
static const int pageDownKey
Definition juce_KeyPress.h:203
static const int numberPadDecimalPoint
Definition juce_KeyPress.h:259
static const int backspaceKey
Definition juce_KeyPress.h:195
static const int numberPadSubtract
Definition juce_KeyPress.h:255
juce_wchar textCharacter
Definition juce_KeyPress.h:272
String getTextDescription() const
Definition juce_KeyPress.cpp:229
Definition juce_ModifierKeys.h:41
static ModifierKeys currentModifiers
Definition juce_ModifierKeys.h:189
@ ctrlModifier
Definition juce_ModifierKeys.h:124
@ commandModifier
Definition juce_ModifierKeys.h:147
@ shiftModifier
Definition juce_ModifierKeys.h:121
@ allKeyboardModifiers
Definition juce_ModifierKeys.h:155
@ altModifier
Definition juce_ModifierKeys.h:127
Definition juce_String.h:53
bool containsIgnoreCase(StringRef text) const noexcept
Definition juce_String.cpp:1045
bool containsChar(juce_wchar character) const noexcept
Definition juce_String.cpp:1040
String retainCharacters(StringRef charactersToRetain) const
Definition juce_String.cpp:1735
String trimEnd() const
Definition juce_String.cpp:1687
static String charToString(juce_wchar character)
Definition juce_String.cpp:359
juce_wchar getLastCharacter() const noexcept
Definition juce_String.cpp:1493
bool endsWith(StringRef text) const noexcept
Definition juce_String.cpp:1421
bool containsWholeWordIgnoreCase(StringRef wordToLookFor) const noexcept
Definition juce_String.cpp:1099
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Definition juce_String.cpp:1565
int getHexValue32() const noexcept
Definition juce_String.cpp:1953
unsigned * m
Definition inflate.c:1559
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
static const char * name
Definition pugl.h:1582
Definition juce_KeyPress.cpp:68
static const char * numberPadPrefix() noexcept
Definition juce_KeyPress.cpp:117
static int getNumpadKeyCode(const String &desc)
Definition juce_KeyPress.cpp:119
static const ModifierDescription modifierNames[]
Definition juce_KeyPress.cpp:104
const KeyNameAndCode translations[]
Definition juce_KeyPress.cpp:75
Definition carla_juce.cpp:31
wchar_t juce_wchar
Definition juce_CharacterFunctions.h:42
constexpr int numElementsInArray(Type(&)[N]) noexcept
Definition juce_MathsFunctions.h:344
Definition inftrees.h:27
Definition juce_KeyPress.cpp:70
int code
Definition juce_KeyPress.cpp:72
const char * name
Definition juce_KeyPress.cpp:71
Definition juce_KeyPress.cpp:99
int flag
Definition juce_KeyPress.cpp:101
const char * name
Definition juce_KeyPress.cpp:100
const char * text
Definition swell-functions.h:167
ZCONST char * key
Definition crypt.c:587
typedef int(UZ_EXP MsgFn)()