gpt4 book ai didi

将 TAB 连接到 gtkEntry

转载 作者:行者123 更新时间:2023-11-30 14:26:48 24 4
gpt4 key购买 nike

如何使 Tab 键在 GtkEntry 中充当 Enter 键?我能够捕捉到“输入”键和“激活”信号。如何捕获 GtkEntry 的“tab”键?

static void enter(GtkWidget *widget, GtkWidget *entry)
{
const gchar *entry_text;
entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
printf("Entry contents: %s\n", entry_text);
}

entry = gtk_entry_new();
g_signal_connect(entry, "activated", G_CALLBACK(enter), entry);

同样的,当按下tab键时如何得到通知?

最佳答案

您可以注册"key-press-event"的条目或"key-release-event"并在事件回调中检查 GdkEventKey 的键值。您可以使用 gdkkeysyms.h 中定义的宏来检查“Tab”或“Enter”,如下所示:

...
#include <gdk/gdkkeysyms.h>
...
static gboolean
key_press_cb(GtkWidget *w, GdkEvent *ev, gpointer data)
{
(void)w; (void)data; /* To avoid compiler warnings */
GdkEventKey *key = (GdkEventKey*)ev;
if(key) /* Extra check maybe redundant */
{
if(key->keyval == GDK_Tab)
printf(" Tab pressed\n");
else if (key->keyval == GDK_Return)
printf(" Enter pressed\n");
}
/* Default handling of "Tab" is change of focus.
* If TRUE is passed here, the event will not be propagated
*/
return FALSE;
}
...
entry = gtk_entry_new ();
g_signal_connect(entry, "key-press-event", G_CALLBACK(key_press_cb), NULL);
...

希望这有帮助!

关于将 TAB 连接到 gtkEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com