如何处理GTK + 3中的键盘事件? [英] How do I handle keyboard events in GTK+3?
问题描述
在GTK + 3中我应该使用什么信号/功能来获得键盘输入?
What signals/functions should I use to get keyboard input in GTK+3?
我环顾四周,并且我所知道的唯一教程涵盖了GTK + 3( zetcode 和 gnome开发人员)似乎并没有涵盖这一点.
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?
推荐答案
感谢您的建议,它确实很有帮助.我迷路了,因此我的问题也许太笼统了,无法逃避C社区一部分的传统否决.我将在这里总结如何在GTK3中处理键盘事件,我希望它会很有用,因为我无法在其他任何地方找到它.
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.
想象一下您正在使用GTK + 3,并且希望您的应用程序在按空格键时可以执行某些操作.这是您的操作方式:
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:
-首先为您的 Gdk.Window 启用 #GDK_KEY_PRESS_MASK 掩码:
-First enable the #GDK_KEY_PRESS_MASK mask for your Gdk.Window:
gtk_widget_add_events(window, GDK_KEY_PRESS_MASK);
-然后,您将窗口与 keyboard_press() 函数连接:
-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);
-一旦按下空格键,将 keyboard_press() 定义为以下内容:
-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;
}
这篇关于如何处理GTK + 3中的键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!