c - X11,使用 XSendEvent(3) 发送事件

标签 c linux x11 xterm motif

我在 Motif 窗口中嵌入了一个 X11 应用程序 (xterm)。一切似乎都工作正常,但如果我在指针不在嵌入式应用程序上方时按下一个键,它就不会得到它。为了解决这个问题,我更改了主应用程序的主循环。现在是这样的:

  XEvent ev;
  for (;;)
    {
      XtAppNextEvent (app, &ev);

      /* If the event is a keypress, send it to
       * the xterm window. Else, dispatch it.
       */
      Window *xtW = NULL, parent, _root;
      unsigned int noC;

      if (ev.type == KeyPress || ev.type == KeyRelease)
   {
#ifdef DEBUG
     fprintf (stderr, "Key event\n");
#endif
     while (xtW == NULL)
       XQueryTree (XtDisplay (drawW), XtWindow (drawW), &_root,
         &parent, &xtW, &noC);

     XSendEvent (XtDisplay (drawW), *xtW, True,
            (KeyPressMask | KeyReleaseMask), &ev);
     XFlush (XtDisplay (drawW));
#ifdef DEBUG
     fprintf (stderr, "sent key event\n");
#endif
   }
      else
     XtDispatchEvent (&ev);
    }

当我在嵌入式 xterm 之外按下一个键时,正在打印调试行,但是 xterm 不打印我发送的 key 。

我该如何解决这个问题?如果需要,我会发布代码。

--mghis

最佳答案

首先,您假设 XQueryTree 只会返回一个子窗口 - 也许它会返回多个子窗口,而您将事件传递给了错误的子窗口?

我不确定围绕 XQueryTree 调用的 while 循环是为了什么。

您还应该在完成后调用 XFree 来释放子窗口列表,否则您将泄漏内存。

关于c - X11,使用 XSendEvent(3) 发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411259/

相关文章:

c - 等待功能不停

c - 为什么我不能初始化我的数组?

linux - 在 linux 脚本上更改目录

linux - 如何检测 Bash/Shell 中 GUI 的可用性?

c++ - 如何使用 Xlib 模拟拖动 Action ?

c++ - C/C++ 如何从 2 个数组中获取唯一值?

c - 如何将数据从 C 传递到汇编

macos - OSX X11 错误无法打开显示

python - 使用python永久添加的linux环境变量

macos - 在 MacOS X 上运行 X11 Server - 并从远程机器连接