I’ve had a problem with ‘popup’ components I want to give keyboard focus. I’ve tryed different approaches using grabKeyboardFocus, setWantsKeyboardFocus, moveKeyboardFocusToSibling etc with no luck.
You here have a simple application illustrating my problem. I can’t write in my textEditor until I have clicked the app with the mouse. Can someone give me an advice?
[code]/* ==============================================================================
- Demonstrating problems keyboard focus in JUCE
- ==============================================================================
*/
#include “includes.h”
class MainComponent : public Component
{
public:
//==============================================================================
MainComponent () : focusLabel (0), textEditor(0) {
addAndMakeVisible (focusLabel = new Label (String::empty,T(“Write your name:”)));
addAndMakeVisible (textEditor = new TextEditor(String::empty));
textEditor->setTextToShowWhenEmpty(T(""),Colours::black);
textEditor->setWantsKeyboardFocus(true);
textEditor->grabKeyboardFocus();
setSize (600, 300);
}
~MainComponent() {
deleteAndZero (focusLabel);
}
void paint (Graphics& g){}
void resized() {
focusLabel->setBounds (152, 80, 296, 48);
textEditor->setBounds (152, 120,296, 48);
}
juce_UseDebuggingNewOperator
private:
Label* focusLabel;
TextEditor* textEditor;
MainComponent (const MainComponent&);
const MainComponent& operator= (const MainComponent&);
};
class FocusTestWindow : public DocumentWindow
{
public:
//==============================================================================
FocusTestWindow()
: DocumentWindow (T(“Focus Test”),
Colours::lightgrey,
DocumentWindow::allButtons,
true)
{
MainComponent* const contentComponent = new MainComponent();
setContentComponent (contentComponent, true, true);
centreWithSize (getWidth(), getHeight());
setVisible (true);
}
~FocusTestWindow() {}
//==============================================================================
void closeButtonPressed()
{
JUCEApplication::quit();
}
};
class FocusTestApplication : public JUCEApplication
{
FocusTestWindow* focusTestWindow;
public:
FocusTestApplication() : focusTestWindow (0) {}
~FocusTestApplication() {}
void initialise (const String& commandLine)
{
focusTestWindow = new FocusTestWindow();
}
void shutdown()
{
if (focusTestWindow != 0) delete focusTestWindow;
}
//==============================================================================
const String getApplicationName() {return T("Focus Test"); }
const String getApplicationVersion() {return T("1.0"); }
bool moreThanOneInstanceAllowed() {return true; }
void anotherInstanceStarted (const String& commandLine) {}
};
START_JUCE_APPLICATION (FocusTestApplication)
[/code]