How do I handle keyboard events in GTK+3?

戏子无情 提交于 2020-01-01 18:54:07

问题


What signals/functions should I use to get keyboard input in GTK+3?

I have looked around and the only tutorials I know of that cover GTK+3 (zetcode and gnome developer) don't seem to cover that.

Anyone can point me in the right direction?


回答1:


Thanks xing for your advice, It was really helpful. I was lost, hence my question that is perhaps too general to escape the traditional downvoting of part of the C community. I will summarize here how to handle keyboard events in GTK3, which I hope will be useful since I cannot find it put together anywhere else.

Imagine you are using GTK+3 and you want your application to do something when you press the space key. This is how you do it:

-First enable the #GDK_KEY_PRESS_MASK mask for your Gdk.Window:

gtk_widget_add_events(window, GDK_KEY_PRESS_MASK);

-Then you connect the window with the keyboard_press() function:

g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (my_keypress_function), NULL);

-Define your keyboard_press() to something once the space key has been pressed:

gboolean my_keypress_function (GtkWidget *widget, GdkEventKey *event, gpointer data) {
    if (event->keyval == GDK_KEY_space){
        printf("SPACE KEY PRESSED!")
        return TRUE;
    }
    return FALSE;
}



回答2:


it's g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (on_key_press), NULL);



来源:https://stackoverflow.com/questions/44098084/how-do-i-handle-keyboard-events-in-gtk3

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