Answer the question
In order to leave comments, you need to log in
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);
...
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()));
}
void MyWidget::show() {
QWidget::show();
SetForegroundWindow(winId());
//SetFocus(winId());
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question