未触发“键释放事件”

时间:2018-09-19 15:12:58

标签: c gtk gtk3

考虑以下代码:

#include <gtk/gtk.h>

static void stop(GtkWidget *window, GdkEventKey *key, gboolean *key_held)
{
        *key_held = FALSE;
        g_print("stopped!\n");
}

static void counter(GtkWidget *window, GdkEventKey *key, gpointer user_data)
{
        gboolean key_held = TRUE;
        gulong signal_ID = g_signal_connect(window, "key-release-event", G_CALLBACK(stop), &key_held);

        for (unsigned long int i = 0;key_held;i++)
        {   
                g_print("%li\n", i); 
        }   

        g_signal_handler_disconnect(window, signal_ID);
}

int main(int argc, char **argv)
{
        gtk_init(&argc, &argv);

        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_widget_add_events(window, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);

        g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
        g_signal_connect(window, "key-press-event", G_CALLBACK(counter), NULL);

        gtk_widget_show(window);

        gtk_main();
}

这很简单:当用户按下某个键时,计数器从0开始计数,并一直计数到用户释放该键...

或者至少这是应该如何工作的。实际发生的情况是,当用户按下某个键时,计数器会启动,并且在用户释放该键时不会停止。停止计数的唯一方法是终止程序。另外,不仅没有触发"key-release-event",也没有触发"delete-event":我必须按 Ctrl + C 终止程序。

0 个答案:

没有答案