Focus Question


#1

When I run my program and I press a key it doesn’t do what it’s supposed to unless I click somewhere on the window to gain focus. I’ve tried using grabKeyboardFocus() and setWantsKeyboardFocus(true) on a bunch of different components but none seem to gain the focus. Is it perhaps grabFocusInternal()? If so can someone explain how to use that function? I’m not sure what to pass in for the first argument. Thank you.

EDIT: Also another thing is I have this code
addAndMakeVisible(objWin);
addAndMakeVisible(segWin);
objWin->setVisible(false);
segWin->setVisible(false);

Now when I run the windows are not visible as I want however when I click it doesn’t appear to gain focus because the F1 and F2 keys do not toggle the visibility of their respective components. What am I doing wrong?


#2

The way to do it is with toFront (true) or grabKeyboardFocus().

Are you sure your window is actually on the screen and visible when you call them? Otherwise the OS calls won’t yet have a window to direct the focus to.


#3

I think that’s what the problem was. I was using the call this->setWantsKeyboardFocus(true) in the constructor. I got it out of the constructor so it’s called after all the initilization is done. Thanks.


#4