gtkgtk3gtkentry

How to capture Enter key press in GtkEntry


I am trying to capture the Enter key when it is pressed in a GtkEntry. Most of the resources I have found suggest connecting to the activate signal, but the documentation for GtkEntry states (emphasis mine):

Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control activation programmatically.

I am curious as to the rationale behind this and also what the generally accepted alternative is. I know that I can connect to the key-press-event signal and check the key code, but I'm wondering if there is a more elegant solution.


Solution

  • It's a keybinding signal. That means that GTK is set up so that when you press Enter in the entry, the signal gets emitted. It can also be emitted programmatically if you want to simulate pressing Enter, so there's no guarantee that the signal was actually the result of a key press.

    If you want to capture a key press, then the best way is to connect to key-press-event.

    That said, I don't see why you can't use the activate signal. Most keybinding signals should not be connected to, but this one seems to make sense. I think you should ask for clarification on the gtk-devel mailing list or file a bug on bugzilla.gnome.org saying that the documentation for this signal should be clarified.