Put keyboard focus on to an EditBox

Started by wizzard, 26 August 2013, 09:52:55


I was using an EditBox in a ChildWindow today and I wanted to make it so that when the ChildWindow pops up, the EditBox would automatically get keyboard focus so that one does not have to explicitly click on the EditBox to start putting text into it.
I assumed that EditBox::Focus would do the job but it only gave the appearance that the EditBox was selected (its border gets darker when selected).
So, I also tried EditBox::enable and EditBox::show but neither of those worked too.
Its really inconvenient to the user to not have this as it is to be expected out of the type of dialog that I am giving.
I'm not sure if this is a bug or a new feature or if I missed how to do it.


Are u using the latest tgui version?

The focus function should do the job, could you post a minimal code of what you are doing (to make sure that we are testing the same thing)?


Ah, it looks like a bug because it works as expected when there is no ChildWindow involved. (using latest version)

#include <TGUI/TGUI.hpp>

#define THEME_CONFIG_FILE "widgets/Black.conf"

int main()
    sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");

    tgui::Gui gui(window);

    if (gui.setGlobalFont("fonts/DejaVuSans.ttf") == false)
        return 1;

    tgui::ChildWindow::Ptr childWindow(gui);
    childWindow->setPosition(20, 20);
    childWindow->setSize(400, 300);

    tgui::EditBox::Ptr editBox(*childWindow);
    editBox->setPosition(40, 200);
    editBox->setSize(300, 30);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
            else if (event.type == sf::Event::TextEntered && event.text.unicode == '`')
                editBox->focus(); // tilde key pressed



    return 0;


This code works for me. The edit box gets focused when I press the '`' key (isn't a tilde '~'?).


It appears focused, but you cannot type into it unless you actually click on it.

At least, that is my problem.


Ah, indeed, you can't type in it.

The ChildWindow has to be focused in order to pass the event to the EditBox, so you should focus the child window as well.
Code (cpp) Select
else if (event.type == sf::Event::TextEntered && event.text.unicode == '`')

Automatically focusing the child window when the edit box gets focused is currently not possible, but it will probably be added in the future.


Yay. It works. I didn't know such a limitation existed.


Actually, it is possible to focus the child window automatically. But I'll have to look into the consequences of such change first.