简体   繁体   中英

How do i get information about the window that has focus on Linux using Mono C#

I already developed an C# .NET WindowsForms application using the Windows user component (user32.dll) that saves the title of the active window (the window that has focus) every time the user changes focus.

Now I'm planning to do the same using Mono C# on Linux. Is it possible?

If yes, for what I'm looking for?

I decided to take a look at the source of gnome-screenshot which has a feature like this (to take a screenshot of the active window, only):

static GdkWindow *
screenshot_find_active_window (void)
{
  GdkWindow *window;
  GdkScreen *default_screen;

  default_screen = gdk_screen_get_default ();
  window = gdk_screen_get_active_window (default_screen);

  return window;
}

It has some logic to fallback to 'the window under the mouse pointer' when the above returns nothing:

GdkWindow *
do_find_current_window (void)
{
  GdkWindow *current_window;
  GdkDeviceManager *manager;
  GdkDevice *device;

  current_window = screenshot_find_active_window ();
  manager = gdk_display_get_device_manager (gdk_display_get_default ());
  device = gdk_device_manager_get_client_pointer (manager);

  /* If there's no active window, we fall back to returning the
   * window that the cursor is in.
   */
  if (!current_window)
    current_window = gdk_device_get_window_at_position (device, NULL, NULL);

  if (current_window)
    {
      if (screenshot_window_is_desktop (current_window))
    /* if the current window is the desktop (e.g. nautilus), we
     * return NULL, as getting the whole screen makes more sense.
         */
        return NULL;

      /* Once we have a window, we take the toplevel ancestor. */
      current_window = gdk_window_get_toplevel (current_window);
    }

  return current_window;
}

All of the above depends exclusive on libgdk-pixbuf as far as I can tell. If that's not an option, you could always look at the implementation of those functions in the source of Gdk.

i'm trying in linux using python to get the mouse clicked/focused window name/title. But i couldn't get any idea.

or any other language can get the window name? is it possible?is there is way for it ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM