Get the foreground window in linux
-
I want to be able to send some event to the foreground window (namely a keypress event( Ctrl+v ) from inside my app.
What I want to is to periodically take the wid of the foregound window( skipping it when my app is active) and then from an action in my app send the ctrl+v event to the last active window.I was able to find this piece of code that posts an event
QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_V, Qt::ControlModifier); QCoreApplication::postEvent (receiver, event);
And for the receiver I am thinking about a QWindow.
There is a function in win32 that does that (GetForegroundWindow()), I am wondering if there is an equivalent for linux.
-
Hi
I don't think QCoreApplication::postEvent can post events to another window in another app.You might need to use native API like
https://stackoverflow.com/questions/39176316/simulating-keystrokes-to-the-active-window-in-linux-using-xsendevent-is-not-workYou could also cheat and use QProcess with
https://unix.stackexchange.com/questions/200469/can-xdotool-be-used-on-some-window-not-in-the-front -
@mrjj said in Get the foreground window in linux:
You might need to use native API like
https://stackoverflow.com/questions/39176316/simulating-keystrokes-to-the-active-window-in-linux-using-xsendevent-is-not-workThis one works, I was even able to find a way to get the foreground window in the code there. Thanks.
But a strange thing happens, I am only being able to send the event(ctrl+v) to the Qt Creator Editor, the other programs like a text editor seem not to be accepting the event.
Edit: Was able to solve the issue. I had to raise the targeted window and set input focus and only then send the keypress.