M
M
Michael2015-08-21 17:20:15
Qt
Michael, 2015-08-21 17:20:15

How to set focus to a window under certain conditions?

Hello!
In general, I have the following problem: there is an application, the main window is hidden, a window is displayed by a key combination and the focus is set to it, when the focus is lost (click outside the window), it is hidden, with another combination, everything is the same, but before the window is displayed, a program imitating the key combination is executed Ctrl+C, in this case the window also appears but does not have focus and therefore cannot lose it when clicking outside the window, which is an inconvenience because it should hide when you click outside of it.
A few notes:
1. the window does not receive focus only after executing the simulation utility Ctrl+C (everything is fine after running the empty utility);
2. if you pause in the display of the window after launching the utility, there will also be no focus;
3. if you display the main window and perform key combinations on it, then the focus is always there;
4. If during the window display pause (after executing the Ctr + C simulation utility) the window is displayed using another key combination, then the focus will be in this window, but after the pause is completed, the window called by the first key combination will still be without focus.
Below are the fragments similar code of the test application:
Here is the code of the utility that imitates the key combination Ctrl + C, the delay to prevent conflict with the combination of which is caused by the launch of this utility in the program, did based on the example from the office. Microsoft site:

...
    Sleep(500);
    // Simulate a key press
    keybd_event(  VK_CONTROL,
                  0x45,
                  KEYEVENTF_EXTENDEDKEY | 0,
                  0 );
    keybd_event(  (BYTE)'C',
                  0x45,
                  KEYEVENTF_EXTENDEDKEY | 0,
                  0 );

    // Simulate a key release
    keybd_event( (BYTE)'C',
                  0x45,
                  KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                  0);
    keybd_event(  VK_CONTROL,
                  0x45,
                  KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                  0);
...

Here is an example of the main program window that is hidden:
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    w = new MyWidget;
    p = new QProcess;

    hot1 = new QxtGlobalShortcut(QKeySequence("Shift+Meta+A"));
    hot2 = new QxtGlobalShortcut(QKeySequence("Shift+Meta+Z"));

  //connect(p,    SIGNAL(finished(int)), SLOT(normalShow()));
    connect(hot1, SIGNAL(activated()),   SLOT(normalShow()));
    connect(hot2, SIGNAL(activated()),   SLOT(afterShow()));
}

Widget::~Widget() { }


void Widget::normalShow() {
    w->show();
}


void Widget::afterShow() {
    p->start("win_xsel/debug/win_xsel.exe");
    QTimer::singleShot(1000*10, this, SLOT(normalShow()));
}

hot1 and hot2 are used for normal display and display after calling the process (utility), the display method is apparently the same, the delay was made to make it clear that at least the imitation of the Ctrl + C combination clearly prevents the window from getting focus, but at the same time when displaying window with the second combination in this interval, the window always has focus, and after the delay, the last one still does not have it.
I was tormented with the problem all day + today, I still didn’t understand what’s the catch that the window has focus if it is called with one combination even after simulating Crtl + C and does not have it if the second, although the display function is the same.
Here is the modified display function:
void MyWidget::show() {
    QWidget::show();
    SetForegroundWindow(winId());
    //SetFocus(winId());
}

Now, if anyone wants to check the source code of the example ( libQxt is needed ): text.zip
I will be glad for any suggestions, now I'm just at a loss from the behavior of the program.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question