Put keyboard focus on to an EditBox
« on: 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.

*

texus

  • *****
  • 1151
    • View Profile
    • Texus's Blog
Re: Put keyboard focus on to an EditBox
« Reply #1 on: 26 August 2013, 10:01:56 »
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)?

Re: Put keyboard focus on to an EditBox
« Reply #2 on: 26 August 2013, 10:20:39 »
Ah, it looks like a bug because it works as expected when there is no ChildWindow involved. (using latest version)

Code: [Select]
#include <TGUI/TGUI.hpp>

#define THEME_CONFIG_FILE "widgets/Black.conf"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");
    window.setVerticalSyncEnabled(true);

    tgui::Gui gui(window);

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

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

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

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

            gui.handleEvent(event);
        }

        window.clear();
        gui.draw();
        window.display();
    }

    return 0;
}
« Last Edit: 26 August 2013, 10:22:38 by wizzard »

*

texus

  • *****
  • 1151
    • View Profile
    • Texus's Blog
Re: Put keyboard focus on to an EditBox
« Reply #3 on: 26 August 2013, 10:42:35 »
This code works for me. The edit box gets focused when I press the '`' key (isn't a tilde '~'?).

Re: Put keyboard focus on to an EditBox
« Reply #4 on: 26 August 2013, 10:44:38 »
It appears focused, but you cannot type into it unless you actually click on it.

At least, that is my problem.

*

texus

  • *****
  • 1151
    • View Profile
    • Texus's Blog
Re: Put keyboard focus on to an EditBox
« Reply #5 on: 26 August 2013, 10:51:37 »
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.
else if (event.type == sf::Event::TextEntered && event.text.unicode == '`')
{
    childWindow->focus();
    editBox->focus();
}

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

Re: Put keyboard focus on to an EditBox
« Reply #6 on: 26 August 2013, 10:53:51 »
Yay. It works. I didn't know such a limitation existed.

*

texus

  • *****
  • 1151
    • View Profile
    • Texus's Blog
Re: Put keyboard focus on to an EditBox
« Reply #7 on: 26 August 2013, 10:58:10 »
Actually, it is possible to focus the child window automatically. But I'll have to look into the consequences of such change first.