49 for (
int i = 0; i < mappings.
size(); ++i)
67 for (
int i = mappings.
size(); --i >= 0;)
71 mappings.
getUnchecked (i)->keypresses.insert (insertIndex, newKeyPress);
80 CommandMapping*
const cm =
new CommandMapping();
81 cm->commandID = commandID;
82 cm->keypresses.add (newKeyPress);
119 addKeyPresses (*
this, ci);
124 if (mappings.
size() > 0)
133 for (
int i = mappings.
size(); --i >= 0;)
147 for (
int i = mappings.
size(); --i >= 0;)
151 for (
int j = cm.keypresses.size(); --j >= 0;)
153 if (keypress == cm.keypresses [j])
155 cm.keypresses.remove (j);
165 for (
int i = mappings.
size(); --i >= 0;)
169 mappings.
getUnchecked (i)->keypresses.remove (keyPressIndex);
179 for (
int i = 0; i < mappings.size(); ++i)
180 if (mappings.getUnchecked (i)->keypresses.contains (keyPress))
181 return mappings.getUnchecked (i)->commandID;
188 for (
int i = mappings.size(); --i >= 0;)
189 if (mappings.getUnchecked (i)->commandID == commandID)
190 return mappings.getUnchecked (i)->keypresses.contains (keyPress);
195void KeyPressMappingSet::invokeCommand (
const CommandID commandID,
197 const bool isKeyDown,
198 const int millisecsSinceKeyPressed,
199 Component*
const originatingComponent)
const
204 info.isKeyDown = isKeyDown;
206 info.millisecsSinceKeyPressed = millisecsSinceKeyPressed;
207 info.originatingComponent = originatingComponent;
209 commandManager.
invoke (info,
false);
232 const CommandID commandId = map->getStringAttribute (
"commandId").getHexValue32();
238 if (map->hasTagName (
"MAPPING"))
242 else if (map->hasTagName (
"UNMAPPING"))
244 for (
auto& m : mappings)
245 if (m->commandID == commandId)
246 m->keypresses.removeAllInstancesOf (key);
261 if (saveDifferencesFromDefaultSet)
264 defaultSet->resetToDefaultMappings();
269 doc->setAttribute (
"basedOnDefaults", saveDifferencesFromDefaultSet);
271 for (
int i = 0; i < mappings.
size(); ++i)
275 for (
int j = 0; j < cm.keypresses.size(); ++j)
277 if (defaultSet ==
nullptr
278 || ! defaultSet->containsMapping (cm.commandID, cm.keypresses.getReference (j)))
280 auto map = doc->createNewChildElement (
"MAPPING");
284 map->setAttribute (
"key", cm.keypresses.getReference (j).getTextDescription());
289 if (defaultSet !=
nullptr)
291 for (
int i = 0; i < defaultSet->mappings.size(); ++i)
293 auto& cm = *defaultSet->mappings.getUnchecked (i);
295 for (
int j = 0; j < cm.keypresses.size(); ++j)
299 auto map = doc->createNewChildElement (
"UNMAPPING");
303 map->setAttribute (
"key", cm.keypresses.getReference (j).getTextDescription());
315 bool commandWasDisabled =
false;
317 for (
int i = 0; i < mappings.
size(); ++i)
321 if (cm.keypresses.contains (key))
333 invokeCommand (cm.commandID, key,
true, 0, originatingComponent);
337 commandWasDisabled =
true;
344 if (originatingComponent !=
nullptr && commandWasDisabled)
355 for (
int i = mappings.
size(); --i >= 0;)
359 if (cm.wantsKeyUpDownCallbacks)
361 for (
int j = cm.keypresses.size(); --j >= 0;)
363 const KeyPress key (cm.keypresses.getReference (j));
366 int keyPressEntryIndex = 0;
367 bool wasDown =
false;
369 for (
int k = keysDown.
size(); --k >= 0;)
373 keyPressEntryIndex = k;
380 if (isDown != wasDown)
386 KeyPressTime*
const k =
new KeyPressTime();
388 k->timeWhenPressed = now;
397 millisecs = (
int) (now - pressTime);
399 keysDown.
remove (keyPressEntryIndex);
402 invokeCommand (cm.commandID, key, isDown, millisecs, originatingComponent);
414 if (focusedComponent !=
nullptr)
One of these objects holds a list of all the commands your app can perform, and despatches these comm...
ApplicationCommandTarget * getTargetForCommand(CommandID commandID, ApplicationCommandInfo &upToDateInfo)
Tries to find the best target to use to perform a given command.
int getNumCommands() const noexcept
Returns the number of commands that have been registered.
const ApplicationCommandInfo * getCommandForID(CommandID commandID) const noexcept
Returns the details about a given command ID.
const ApplicationCommandInfo * getCommandForIndex(int index) const noexcept
Returns the details about one of the registered commands.
bool invoke(const ApplicationCommandTarget::InvocationInfo &invocationInfo, bool asynchronously)
Sends a command to the default target.
String getDescriptionOfCommand(CommandID commandID) const noexcept
Returns the description field for a command.
Holds a resizable array of primitive or copy-by-value objects.
Holds a list of ChangeListeners, and sends messages to them when instructed.
void sendChangeMessage()
Causes an asynchronous change message to be sent to all the registered listeners.
static bool isUpperCase(juce_wchar character) noexcept
Checks whether a unicode character is upper-case.
The base class for all JUCE user-interface objects.
virtual bool keyStateChanged(bool isKeyDown)
Called when a key is pressed or released.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
void removeFocusChangeListener(FocusChangeListener *listener)
Unregisters a FocusChangeListener that was added with addFocusChangeListener().
void addFocusChangeListener(FocusChangeListener *listener)
Registers a FocusChangeListener that will receive a callback whenever the focused component changes.
static Desktop &JUCE_CALLTYPE getInstance()
There's only one desktop object, and this method will return it.
Classes can implement this interface and register themselves with the Desktop class to receive callba...
Receives callbacks when keys are pressed.
Manages and edits a list of keypresses, which it uses to invoke the appropriate command in an Applica...
void addKeyPress(CommandID commandID, const KeyPress &newKeyPress, int insertIndex=-1)
Assigns a keypress to a command.
void resetToDefaultMapping(CommandID commandID)
Resets all key-mappings to the defaults for a particular command.
Array< KeyPress > getKeyPressesAssignedToCommand(CommandID commandID) const
Returns a list of keypresses that are assigned to a particular command.
bool containsMapping(CommandID commandID, const KeyPress &keyPress) const noexcept
Returns true if the given command is linked to this key.
void removeKeyPress(CommandID commandID, int keyPressIndex)
Removes one of the keypresses that are assigned to a command.
~KeyPressMappingSet() override
Destructor.
void globalFocusChanged(Component *) override
Callback to indicate that the currently focused component has changed.
void clearAllKeyPresses()
Removes all keypresses that are assigned to any commands.
void resetToDefaultMappings()
Reset all mappings to the defaults, as dictated by the ApplicationCommandManager.
CommandID findCommandForKeyPress(const KeyPress &keyPress) const noexcept
Looks for a command that corresponds to a keypress.
bool keyPressed(const KeyPress &, Component *) override
Called to indicate that a key has been pressed.
KeyPressMappingSet(ApplicationCommandManager &)
Creates a KeyPressMappingSet for a given command manager.
bool keyStateChanged(bool isKeyDown, Component *) override
Called when any key is pressed or released.
bool restoreFromXml(const XmlElement &xmlVersion)
Tries to recreate the mappings from a previously stored state.
std::unique_ptr< XmlElement > createXml(bool saveDifferencesFromDefaultSet) const
Creates an XML representation of the current mappings.
Represents a key press, including any modifier keys that are needed.
juce_wchar getTextCharacter() const noexcept
Returns the character that is associated with this keypress.
bool isValid() const noexcept
Returns true if this is a valid KeyPress.
bool isCurrentlyDown() const
Checks whether the user is currently holding down the keys that make up this KeyPress.
ModifierKeys getModifiers() const noexcept
Returns the key modifiers.
static KeyPress createFromDescription(const String &textVersion)
Converts a textual key description to a KeyPress.
virtual void playAlertSound()
Plays the system's default 'beep' noise, to alert the user about something very important.
bool isShiftDown() const noexcept
Checks whether the shift key's flag is set.
int size() const noexcept
Returns the number of items currently in the array.
ObjectClass * getUnchecked(int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
void remove(int indexToRemove, bool deleteObject=true)
Removes an object from the array.
void clear(bool deleteObjects=true)
Clears the array, optionally deleting the objects inside it first.
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
Used to build a tree of elements representing an XML document.
bool getBoolAttribute(StringRef attributeName, bool defaultReturnValue=false) const
Returns the value of a named attribute as a boolean.
bool hasTagName(StringRef possibleTagName) const noexcept
Tests whether this element has a particular tag name.
Iterator< GetNextElement > getChildIterator() const
Allows iterating the children of an XmlElement using range-for syntax.
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
int CommandID
A type used to hold the unique ID for an application command.
Holds information describing an application command.
int flags
A bitwise-OR of the values specified in the CommandFlags enum.
Array< KeyPress > defaultKeypresses
A list of zero or more keypresses that should be used as the default keys for this command.
CommandID commandID
The command's unique ID number.
@ wantsKeyUpDownCallbacks
If this flag is present, then when a KeyPressMappingSet invokes the command, it will call the command...
@ isDisabled
Indicates that the command can't currently be performed.
Contains contextual details about the invocation of a command.
@ fromKeyPress
The command is being invoked by a key-press.