如何在GtkEntry中使Tab键充当Enter键?
我能够用“已激活”信号捕获“ 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);
...


希望这可以帮助!

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

10-16 13:51