How to use gdk_device_get_position()?
Asked Answered
A

2

7

I am trying to get the pointer position on screen in Gdk and found gdk_display_get_pointer(), which works fine, but it's marked as deprecated and refers to gdk_device_get_position() now.

But how do I use this function? I cannot get a GdkDevice, since there is no factory, nor is there a constructor.

Antiperspirant answered 19/7, 2014 at 20:5 Comment(0)
K
9

use Gdk.DeviceManager.

.....
.....
GdkDisplay *display = gdk_display_get_default ();
GdkDeviceManager *device_manager = gdk_display_get_device_manager (display);
GdkDevice *device = gdk_device_manager_get_client_pointer (device_manager);

// do whatever with Gdk.Device, i.e:
int x, y;
gdk_device_get_position (device, NULL, &x, &y);
printf ("x= %d, y=%d", x,y);
Kibbutz answered 20/7, 2014 at 3:40 Comment(2)
Thought I might add for anyone interested in getting the cursor position in the window, you can use gdk_window_get_device_position (gtk_widget_get_window(gtk_window), device, &x, &y, NULL); Firstly
Should I worry about the speed of calling these three functions? In the end I need to call gdk_window_get_device_position, as jackw11111 remarked, but I only have a GtkDrawingArea (widget). It seems a bit strange to be forced to call gdk_window_get_device_position(gtk_widget_get_window(my_widget), gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gdk_display_get_default()), &x, &y, NULL); just to get the x and y in my drawing area :/Furtherance
F
6

Since GTK+ 3.20+, luciomrx's answer becomes:

GdkDisplay* display = gdk_display_get_default();
GdkSeat* seat = gdk_display_get_default_seat(display);
GdkDevice* pointer = gdk_seat_get_pointer(seat);
Furtherance answered 17/7, 2020 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.