1

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 ?

3 Answers 3

1

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.

3
  • In fact I already have done the step 1, but what I really miss, is how can I compare two GdkWindow, I can't use the pointers for that. And not always this GdkWindow is the toplevel, sometimes it is another window, I even tried to use g_object_set_data to add a name to the window, but as I cited, this window isn't always the toplevel. How can I always get the toplevel and how can I get the XID for the GdkWindow ? Great thanks for your answer.
    – Tarantula
    Dec 8, 2010 at 11:12
  • I've found gdk_window_get_toplevel and GDK_WINDOW_XID, I'll take a look if it works !
    – Tarantula
    Dec 8, 2010 at 11:15
  • Hey @Tarantula, did you ever find out a solution to this? I am trying to do the same thing, 5 years later. Please let me know if you found a trick for it
    – TuxForLife
    Oct 23, 2015 at 13:52
1

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.

1

gtk_window_set_keep_above(a) followed by gtk_window_present(b)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.