Query cursor position with GTK

不问归期 提交于 2021-01-29 07:57:32

问题


Is there a way to query the cursor position using the GTK C libraries? I am writing a program where I need to read out the position of the cursor in real time (either through a callback function or by polling the values to look for change). I have scanned through the GTK documentation, but I couldn't find anything obvious that exposes the cursor position.

I am currently writing this program using the GTK C libraries because a) I already have some code written in C to interface with the low level Raspberry Pi peripherals, and I'd rather not rewrite it for a different language if I can avoid it. The low level code is based on mmap(), which I believe still works with C++, so if push comes to shove, I supposed I can rewrite it in C++ with some other GUI library like wxWidgets or QT. (Are there easy ways to read the cursor position with those libraries?)


回答1:


You can take a look at how it's done in those toy widgets which draw eyes on the panel, e.g. xfce4-eyes-plugin.

gboolean
where (void)
{
  gint x, y;
  GdkWindow *window;
  GdkDevice *mouse_device;

#if GTK_CHECK_VERSION (3,20,0)
  GdkSeat *seat = gdk_display_get_default_seat (gdk_display_get_default ());
  mouse_device = gdk_seat_get_pointer (seat);
#else
  GdkDeviceManager *devman = gdk_display_get_device_manager (gdk_display_get_default ());
  mouse_device = gdk_device_manager_get_client_pointer (devman);
#endif

  window = gdk_display_get_default_group (gdk_display_get_default ());
  gdk_window_get_device_position (window, mouse_device, &x, &y, NULL);
  g_message ("pointer: %i %i", x, y);

  return G_SOURCE_CONTINUE;
}


来源:https://stackoverflow.com/questions/55213291/query-cursor-position-with-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!