我正在使用GTK库编写C程序,该程序读取游标的位置并将其写到另一个函数中。我已经编写了能够准确读取光标位置数据的代码,但是我不知道如何将光标运动事件连接到函数。
这是我到目前为止的代码:
int main(int argc, char **argv){
GdkWindow *window;
GtkWidget *gtk_window;
GdkDevice *mouse;
gint x = 0;
gint y = 0;
gdk_init(&argc, &argv);
gtk_init(&argc, &argv);
gtk_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(gtk_window);
window = gtk_widget_get_window(GTK_WIDGET(gtk_window));
gdk_window_maximize(window);
gdk_window_set_title(window, "write-mouse");
GdkSeat *seat = gdk_display_get_default_seat (gdk_display_get_default ());
mouse = gdk_seat_get_pointer (seat);
gdk_window_get_device_position (window, mouse, &x, &y, NULL);
//read cursor position at startup to test code
g_print("X = %d\n", x);
g_print("Y = %d\n", y);
//g_signal_connect (mouse, "something?", G_CALLBACK (print_mouse), NULL);
gtk_main();
}
static void print_mouse(GdkDevice *mouse, gpointer data){
gint x = 0;
gint y = 0;
//do I need to pass the window object to "print_mouse" function too?
//can I point to it using some gtk function?
gdk_window_get_device_position (window, mouse, &x, &y, NULL);
g_print("X = %d\n", x);
g_print("Y = %d\n", y);
}
我假设我需要使用“ g_signal_connect()”将光标运动事件连接到“ print_mouse”函数,但是我不知道要放置什么作为使它起作用的参数。
我应该在“ g_signal_connect()”的“ detailed_signal”字段中输入什么?
有没有更好的方法可以完全做到这一点?
答案 0 :(得分:0)
您的东西是“ motion-notify-event”,它将数据作为GdkEventMotion事件提供:https://developer.gnome.org/gtk2/stable/GtkWidget.html#GtkWidget-motion-notify-event
我认为您还需要确保gtk_widget_add_events(widget,GDK_POINTER_MOTION_MASK);在构建窗口小部件时。出于性能原因,您可能希望将其设置为仅以一定的刷新率运行代码,以免使CPU负担过重。