Search code examples
cgtk

How to pass the entries using enter in GTK?


I'm learning programming with GTK, so I have a doubt... how can I jump from one entry to other ?

I'm with the cursor in the field "a", so I put a text in this field and pass to other without use TAB key. there's a function, a signal to it ?

// I'm with the cursor in this entry below
    code_field = gtk_entry_new();
//so i want to press enter and jump to this
    name_field = gtk_entry_new();```

Solution

  • You can use g_signal_connect() with gtk_widget_grab_focus()

    g_signal_connect Documentation

    gtk_widget_grab_focus Documentation

    Example:

    assuming we have 3 variables, where you want change focus from entry1 to entry2 when enter is pressed on entry1.

    GtkEntry *entry1;
    
    GtkEntry *entry2;
    
    void change_focus_func();
    

    You should create an g_signal for the entry1, and connect it on change_focus_func, passing entry2 as 4º argument.

    g_signal_connect(GTK_ENTRY(entry1),"activate",G_CALLBACK(change_focus_func),entry2);
    

    Then function change_focus_func should be like:

    void  change_focus_func(GtkWidget *Widget1, Widget2){
      gtk_widget_grab_focus(Widget2);
    }