Search code examples
linuxwxwidgetsx11

Sending keyboard events to other windows from wxwidget app in linux


I'm writing a linux application using C++ and wxWidgets.
From my application, I need to send keyboard events to the window that currently has the focus (not belonging to my application!).

My questions are:

  1. How can I find out what window has the focus?
  2. How can I send a keyboard event to a window not belonging to my application?

Thanks
Daniele


Solution

    1. XGetInputFocus(3).
    2. X11 does not care about "applications". There are only windows. It's enough to have a window ID (Window is the Xlib data type). Use whatever method of sending events works. There are two methods I know of: XSendEvent(3) and XTestFakeKeyEvent(3). The former method does not work with some programs that chose to ignore events coming from XSendEvent. The latter one requires the XTest extension, which is present in most, but not all, modern servers.
    3. Note that InputFocus is a valid window designator for XSendEvent, and XTestFakeKeyEvent is delivered to the window that has the focus anyway, so you probably don't need to call XGetInputFocus at all.