Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get the foreground window in linux
Qt 6.11 is out! See what's new in the release blog

Get the foreground window in linux

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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-work

      You could also cheat and use QProcess with
      https://unix.stackexchange.com/questions/200469/can-xdotool-be-used-on-some-window-not-in-the-front

      H 1 Reply Last reply
      3
      • mrjjM mrjj

        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-work

        You could also cheat and use QProcess with
        https://unix.stackexchange.com/questions/200469/can-xdotool-be-used-on-some-window-not-in-the-front

        H Offline
        H Offline
        hbatalha
        wrote on last edited by hbatalha
        #3

        @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-work

        This 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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved