Keys Ctrl+S not working

Hey there,

I am writing an application and would like to add some shortcuts like “Ctrl+S” for saving, but it doesn’t work. If I just press the “s” key, I will get the value ‘115’ in ‘temp’, so without ctrl everything works fine:

bool MainStage::keyPressed(const KeyPress& k, Component* c)
{
    auto temp = k.getTextCharacter();
    if (temp == 's')
    {
        saveProject();
    }

    return true;
}

But if I press ctrl and then “s” (or another key) the ‘temp’ value is always ‘0’. I thought I have to do something like this:

bool MainStage::keyPressed(const KeyPress& k, Component* c)
{
    auto temp = k.getTextCharacter();

    if (ModifierKeys::getCurrentModifiers().isCtrlDown())
    {
        if (temp == 's')
        {
            saveProject();
        }
    }   

    return true;
}

but this is pointless as long as the temp value is 0. What am I doing wrong?

The docs for getTextCharacter() says:

This is the character that you’d expect to see printed if you press this keypress in a text editor or similar component.

I think instead you should use isKeyCode():

Checks whether the KeyPress’s key is the same as the one provided, without checking the modifiers.

It makes sense if you consider in a text editor, Ctrl+S wouldn’t show up as ‘s’.

Apart from that, I wanted to pull your attention to the ApplicationCommandManager. This has a convenient method to catch keyboard shortcuts and to let the user create a mapping, see getKeyMappings()

All right. Got it! Thanks a lot. :slight_smile: