How to trigger a hotkey to a QWindow using a QButton?
-
After created a QWindow using a window ID with
QWindow *window = QWindow::fromWinId(123456);
And put it inside a widget using
QWidget *widget = QWidget::createWindowContainer(window); QVBoxLayout *layout = new QVBoxLayout(ui->anotherWidget); layout->addWidget(widget); ui->anotherWidget->setLayout(layout);
I need to active that QWindow and trigger a hotkey when user click on a QButton (this button is already on GUI). In case, the QWindow could be a text editor. There will be a button "select all" that when user click on it, should active the QWindow and trigger the hotkey "ctrl+a". There is a way to do this?
Could be something like this:
// button's event function void GUI::on_selectAll_clicked() { window->triggerHotkey("ctrl", "a"); }
But QWindow doesnt have this function and i didnt find any that do something like that.
-
Send the event to your widget, not to your window.
MaybeQShortCut
works for you or simply create aQAction
and assign the key shortcut. -
@Pl45m4 said in How to trigger a hotkey to a QWindow using a QButton?:
Send the event to your widget, not to your window.
MaybeQShortCut
works for you or simply create aQAction
and assign the key shortcut.But QShortCut is not used when the user types the key? When the user click on a button, the program should simulate a hotkey "ctrl+a" to another widget. The user will not type it.
-
You can trigger
QShortCut
orQActions
without typing the actual keys and send it to the destination widget.Or you can create a
QKeyEvent
and send to to your widget -
I have some really noob questions... Im trying to use QKeyEvent and what I need is to sendo a pair of keys, like ctrl+a, ctrl+v, etc. The question is about the constructor's parameters...
I have here
QKeyEvent *keyEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Control, Qt::NoModifier)
Whats difference between KeyPress and KeyRelease?
And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A
-
Hi,
@fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:
Whats difference between KeyPress and KeyRelease?
The first when you press a key on the keyboard.
The second when you release the key from the keyboard.@fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:
And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A
A is the key, Ctrl is the modifier.
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Hi,
@fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:
Whats difference between KeyPress and KeyRelease?
The first when you press a key on the keyboard.
The second when you release the key from the keyboard.@fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:
And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A
A is the key, Ctrl is the modifier.
Aaah got it. I understood the key and modifier
About press and release a key… it means i need two event objects? One for press and another for release to send to a widget?
-
Yes you do.
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Yes you do.
I tried this code: two events inside a button clicked event
void Simulate::on_selectAllBtn_clicked() { QKeyEvent *eventPress = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier); QKeyEvent *eventRealease = new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier); QCoreApplication::postEvent(targetWidget, eventPress); QCoreApplication::postEvent(targetWidget, eventRealease); }
But I got a error on "KeyPress" and "KeyRelease": excepted unqualified-id before numeric constant
Can you help me with that?
-
Can you show the compiler error message please ?
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Can you show the compiler error message please ?
Yes, sure.
Compile Output in Qt Creator
Detail: Changed button's name to exportBtn_2 and targetWidget to simulationWidget, but the error persists
-
Did you include QEvent ?
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Did you include QEvent ?
Yes, i do.
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Did you include QEvent ?
I got the solution. I was doing #include <xdotool-master/xdo.h> and i think this file was causing some type of conflict. I just commented this line.
I gonna back to the event's problem
-
void Simulate::on_selectAllBtn_clicked() { QKeyEvent *eventPress = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier); QKeyEvent *eventRealease = new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier); QCoreApplication::postEvent(targetWidget, eventPress); QCoreApplication::postEvent(targetWidget, eventRealease); }
This solution doesnt work. Is this code correct? As I commented, I created a QWindow from a window ID of a external program (in case, a gedit text editor) and put it on a QWidget. I dont know if this approach is for my case. I tried too use QApplication::setActiveWIndow and targetWidget->setFocus() before, but without success. Tried send the event to QWindow too, without success.
-
You are trying to send these events to an external application ?
Which is it ? -
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
You are trying to send these events to an external application ?
Which is it ?Kind of it. I have a gedit text editor (external application) embed to a QWindow (intern application). And a QWidget is created from this QWindow, using QWidget *widget = QWidget::createWindowContainer(window);
In addition to that widget, i have too, on GUI, a QButton, called "Select All". When user click on this button, the system should select all text in text editor. So Im trying to send a Ctrl+A under the covers.
But I dont know if Im on right way to do this using that code.
-
Which OS are you on ?
Sending key events through Qt's event loop like that won't work AFAIK. The application while embedded is still an external application.
-
@SGaist said in How to trigger a hotkey to a QWindow using a QButton?:
Which OS are you on ?
Sending key events through Qt's event loop like that won't work AFAIK. The application while embedded is still an external application.Im using Ubuntu 18.04.
There is a way to send the key event through OS?