JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
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
34KeyPress::KeyPress (const int code) noexcept : keyCode (code)
35{
36}
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
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//==============================================================================
67namespace KeyPressHelpers
68{
70 {
71 const char* name;
72 int code;
73 };
74
75 const KeyNameAndCode translations[] =
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
104 static const ModifierDescription modifierNames[] =
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 {
121 if (desc.containsIgnoreCase (numberPadPrefix()))
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
150 {
151 const char* text;
152 juce_wchar symbol;
153 };
154
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
178 for (int i = 0; i < numElementsInArray (KeyPressHelpers::modifierNames); ++i)
179 if (desc.containsWholeWordIgnoreCase (KeyPressHelpers::modifierNames[i].name))
180 modifiers |= KeyPressHelpers::modifierNames[i].flag;
181
182 int key = 0;
183
184 for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
185 {
186 if (desc.containsWholeWordIgnoreCase (String (KeyPressHelpers::translations[i].name)))
187 {
188 key = KeyPressHelpers::translations[i].code;
189 break;
190 }
191 }
192
193 if (key == 0)
194 key = KeyPressHelpers::getNumpadKeyCode (desc);
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
250 for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
251 if (keyCode == KeyPressHelpers::translations[i].code)
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);
258 else if (keyCode >= numberPad0 && keyCode <= numberPad9) desc << KeyPressHelpers::numberPadPrefix() << (keyCode - numberPad0);
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";
265 else if (keyCode == numberPadDecimalPoint) desc << KeyPressHelpers::numberPadPrefix() << '.';
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
static juce_wchar toLowerCase(juce_wchar character) noexcept
Converts a character to lower-case.
static juce_wchar toUpperCase(juce_wchar character) noexcept
Converts a character to upper-case.
Represents a key press, including any modifier keys that are needed.
static const int numberPadMultiply
key-code for the multiply sign on the numeric keypad.
String getTextDescriptionWithIcons() const
Creates a textual description of the key combination, using unicode icon symbols if possible.
static const int playKey
key-code for a multimedia 'play' key, (not all keyboards will have one)
static const int F35Key
key-code for the F35 key
static const int homeKey
key-code for the home key
static const int F24Key
key-code for the F24 key
static const int numberPadEquals
key-code for the equals key on the numeric keypad.
static const int upKey
key-code for the cursor-up key
KeyPress()=default
Creates an (invalid) KeyPress.
static const int F16Key
key-code for the F16 key
static const int fastForwardKey
key-code for a multimedia 'fast-forward' key, (not all keyboards will have one)
static const int stopKey
key-code for a multimedia 'stop' key, (not all keyboards will have one)
static const int numberPadSeparator
key-code for the comma on the numeric keypad.
static bool isKeyCurrentlyDown(int keyCode)
Checks whether a particular key is held down, irrespective of modifiers.
static const int endKey
key-code for the end key
static const int F1Key
key-code for the F1 key
static const int tabKey
key-code for the tab key
static const int numberPad0
key-code for the 0 on the numeric keypad.
static const int numberPad9
key-code for the 9 on the numeric keypad.
static const int numberPadAdd
key-code for the add sign on the numeric keypad.
static const int rightKey
key-code for the cursor-right key
static const int F17Key
key-code for the F17 key
static const int rewindKey
key-code for a multimedia 'rewind' key, (not all keyboards will have one)
static const int deleteKey
key-code for the delete key (not backspace)
static const int insertKey
key-code for the insert key
static const int F25Key
key-code for the F25 key
bool operator==(const KeyPress &other) const noexcept
Compares two KeyPress objects.
bool isCurrentlyDown() const
Checks whether the user is currently holding down the keys that make up this KeyPress.
static const int downKey
key-code for the cursor-down key
static const int numberPadDelete
key-code for the delete key on the numeric keypad.
static KeyPress createFromDescription(const String &textVersion)
Converts a textual key description to a KeyPress.
static const int spaceKey
key-code for the space bar
static const int escapeKey
key-code for the escape key
static const int numberPadDivide
key-code for the divide sign on the numeric keypad.
static const int returnKey
key-code for the return key
static const int leftKey
key-code for the cursor-left key
static const int pageUpKey
key-code for the page-up key
static const int pageDownKey
key-code for the page-down key
static const int numberPadDecimalPoint
key-code for the decimal point sign on the numeric keypad.
bool operator!=(const KeyPress &other) const noexcept
Compares two KeyPress objects.
static const int backspaceKey
key-code for the backspace key
static const int numberPadSubtract
key-code for the subtract sign on the numeric keypad.
String getTextDescription() const
Creates a textual description of the key combination.
Represents the state of the mouse buttons and modifier keys.
static ModifierKeys currentModifiers
This object represents the last-known state of the keyboard and mouse buttons.
bool isAltDown() const noexcept
Checks whether the ALT key's flag is set.
bool isCtrlDown() const noexcept
Checks whether the CTRL key's flag is set.
bool isCommandDown() const noexcept
Checks whether the 'command' key flag is set (or 'ctrl' on Windows/Linux).
int getRawFlags() const noexcept
Returns the raw flags for direct testing.
bool isShiftDown() const noexcept
Checks whether the shift key's flag is set.
@ ctrlModifier
CTRL key flag.
@ commandModifier
Command key flag - on windows this is the same as the CTRL key flag.
@ shiftModifier
Shift key flag.
@ allKeyboardModifiers
Represents a combination of all the shift, alt, ctrl and command key modifiers.
@ altModifier
ALT key flag.
The JUCE String class!
Definition juce_String.h:53
bool containsChar(juce_wchar character) const noexcept
Tests whether the string contains a particular character.
String retainCharacters(StringRef charactersToRetain) const
Returns a version of this string that only retains a fixed set of characters.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
static String charToString(juce_wchar character)
Creates a string from a single character.
juce_wchar getLastCharacter() const noexcept
Returns the final character of the string.
bool containsWholeWordIgnoreCase(StringRef wordToLookFor) const noexcept
Tests whether the string contains another substring as a distinct word.
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from a given substring.
int getHexValue32() const noexcept
Parses the string as a hexadecimal number.
typedef int
JUCE Namespace.
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.