问题
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