TextEditor::keyPressed not receiving printable character key presses

The TextEditor::keyPressed method doesn’t seem to receive any key presses that are printable characters. I must be misunderstanding how this function is supposed to work…

I’ve subclassed TextEditor and overridden the keyPressed() method. On any key press that’s a printable character, that method never gets called. However it does get called on other non-printable character keys, like ‘delete’, ‘enter’, and ‘tab’.

I’m just trying to intercept the text editors key presses in order to evaluate what the key pressed actually was. Am I doing something wrong here?

bool AutoCompleteEditor::keyPressed(const KeyPress &key)
{
    auto character = key.getTextCharacter();
    keyWasPressed = CharacterFunctions::isPrintable(character);
    
    return TextEditor::keyPressed(key);
}

Are you sure your TextEditor subclass has Focus?

The Component keyPressed API is quite basic, and isn’t suitable for multilingual text input. Depending on the input language, mutliple key presses might correspond to a single glyph (Japanese, Korean etc.). The rules here are extremely complex, so JUCE delegates text composition to the operating system.

JUCE text input components communicate with the OS via the juce::TextInputTarget API. The current cursor position is set via setHighlightedRegion, and new text is inserted via insertTextAtCaret. If you’re trying to detect when new text is added to the text editor, these are the functions you should override.

1 Like

Ok, good to know. I’ll do as you suggest then and override insertTextAtCaret. Thanks!

Ya, the editor definitely has focus. I guess it’s like @reuk said, the OS is handling text input so there’s other factors at play. I’ve only tried this on Mac so far but I wonder then if Windows might behave differently. I guess bottom line is I can’t rely on that method unfortunately.