如何在GTK + 3中处理键盘事件?(How do I handle keyboard events in GTK+3?)

编程入门 行业动态 更新时间:2024-10-20 20:39:00
如何在GTK + 3中处理键盘事件?(How do I handle keyboard events in GTK+3?)

我应该用什么信号/功能在GTK + 3中输入键盘?

我环顾四周,我所知道的唯一涵盖GTK + 3( zetcode和gnome开发人员 )的教程似乎并没有涵盖这一点。

任何人都可以指出我正确的方向?

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?

最满意答案

谢谢你的建议,这真的很有帮助。 我迷路了,因此我的问题可能过于笼统,无法摆脱C社区部分传统的贬低。 我将在这里总结一下如何在GTK3中处理键盘事件,我希望这会有用,因为我无法在其他任何地方找到它。

想象一下,您正在使用GTK + 3,并且您希望应用程序在按空格键时执行某些操作。 这是你如何做到的:

-First为您的Gdk.Window启用#GDK_KEY_PRESS_MASK掩码:

gtk_widget_add_events(window, GDK_KEY_PRESS_MASK);

- 然后使用keyboard_press()函数连接窗口:

g_signal_connect (G_OBJECT (window), "keyboard_press", G_CALLBACK (on_key_press), NULL);

- 按下空格键后,将keyboard_press()定义为某些内容:

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

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; }

更多推荐

本文发布于:2023-08-07 07:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键盘   事件   如何在   GTK   events

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!