GtkEntry with GtkEventControllerKey not working

Hi, I start develop with C and Gtk4. Now I have a little problem. Gtk Event Controller Key works fine with TextView and Button but not with GtkEntry. Can’t find anything in the web. Have you a idea? Thank you.

static gboolean test(  GtkEventControllerKey *controller,
                                            guint                           keyval,
                                            guint                           keycode,
                                            GdkModifierType  state,
                                            gpointer                    user_data)
   g_print("%i --- %i\n", keyval, keycode );
   return FALSE;

 static void app_activate (GApplication *app, gpointer user_data)
   GtkWidget          *win, *tv;
   GtkEventController *contr = gtk_event_controller_key_new();

   win = gtk_application_window_new( GTK_APPLICATION( app ) );
   gtk_window_set_title( GTK_WINDOW( win ), "Test");
   gtk_window_set_default_size( GTK_WINDOW( win ), 200, 150 );
   //tv = gtk_button_new_with_label("test");  // OK
   //tv = gtk_text_view_new ();                               // OK
   tv = gtk_entry_new();                                            // NOT OK ?!
   gtk_widget_set_focusable( GTK_WIDGET( tv ), TRUE );
   gtk_widget_add_controller( tv , contr );
   gtk_window_set_child( GTK_WINDOW( win ), tv );
   g_signal_connect( contr, "key-pressed", G_CALLBACK( test ), tv );
   gtk_widget_show( win );

GtkEntry already has a key event controller, so you’re going to fight against it.

I guess the real question is: what is it that you’re trying to achieve?

I would like to allow only digits or letters or implement “regex” for controlled input.

Then you probably want to connect to the GtkEditable::insert-text signal instead of trying to intercept key events.

Oh I see. This is the right way. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.