Gtk:将焦点发送到顶级窗口而不丢失第一个顶级窗口

发布于 2024-10-06 15:44:47 字数 98 浏览 0 评论 0原文

情况如下:

1)我有两个顶级窗口,A 和 B

2)A 在 B 前面

如何将键盘焦点发送到窗口 B,同时保持窗口 A 在 B 前面?

Here is the situation:

1) I have two toplevel windows, A and B

2) A is in front of B

How can I send to keyboard focus to the window B while keeping the window A in front of B ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-10-13 15:44:47

我假设您控制两个窗口,并且这是在像 Linux 这样的 X11 系统上。如果没有,那就更具挑战性。我在一个应用程序中完成了类似的事情,这里有一些回忆。

您可能已经发现不能只使用 gtk_widget_grab_focus() 来做到这一点。这只适用于当窗口本身具有焦点时确定窗口中的哪个小部件具有焦点。

X11 根据窗口层次结构、来自窗口管理器的信息等来确定哪个窗口获取键盘事件。但是,您可以通过 GDK 进行修改以获得您想要的结果。

您必须了解 GDK 事件传播,并且可能阅读一些 GDK 源代码。但我相信,一般来说,您需要做的是:

  1. 使用 gdk_event_handler_set() 安装您自己的事件处理程序。您需要在 GTK+ 初始化后执行此操作,并链接到 gtk_main_do_event()。
  2. 当获得键盘事件(GdkEventKey)时,查看X事件结构。如果它具有窗口 A 的 XID,请将其替换为窗口 B 的 XID,并将其传递给 GTK+。您可能需要复制该事件,而不是修改原始事件。

如果窗口属于不同的应用程序,您可以查看 gdk_event_send_client_message(),但我从未使用过它。

I'm assuming you control both windows, and this is on an X11 system like Linux. If not, it's much more challenging. I've done things like this within a single app, and here are some recollections.

You've probably figured out you can't just use gtk_widget_grab_focus() to do it. That only works for determining which widget within a window has focus when the window itself has focus.

It's X11 that determines which window gets a keyboard event, based on the window hierarchy, info from the window manager, etc. However, you can monkey around with that via GDK to get the result you want.

You'll have to learn about GDK event propagation, and probably read some of the GDK sources. But I believe that, generally, what you'll need to do is this:

  1. Use gdk_event_handler_set() to install your own event handler. You'll need to do this after GTK+ is initialized, and chain to gtk_main_do_event().
  2. When you get a keyboard event (GdkEventKey), look at the X event structure. If it has the XID for window A, replace that with the XID for window B, and pass it on to GTK+. You might need to duplicate the event, and not modify the original one.

If the windows belong to different apps, you can look at gdk_event_send_client_message(), but I've never used it.

差↓一点笑了 2024-10-13 15:44:47

如果您不介意它不是直接的,您可以将键盘事件从顶层窗口发送到其后面的窗口。当然,这是假设两个窗口都是由您创建的,而不是编写一个程序来悬停在后台并读取单独程序上使用的键盘输入。

If you don't mind that it's not direct, you could send the keyboard events from the top level window to the one behind it. Of course that assumes that both windows are created by you rather than writing a program to hover in the background and read keyboard input being used on a separate program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文