Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture Enter key press in GtkEntry

Tags:

gtk

gtk3

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.

like image 470
Sean Bright Avatar asked Dec 15 '11 13:12

Sean Bright


2 Answers

The mentioned GtkEntry documentation for the activate signal:

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

...has been changed to:

While this signal is used as a keybinding signal, it is also commonly used by applications to intercept activation of entries.

This happened on 2012-06-11 with commit 4a25bac0e7685000fff90a211db6ac60f6b74ab1. The commit message is:

Update docs for GtkEntry::activate

Remove the 'you shall not connect' message from this signal. While it is a keybinding signal, using it from applications is fine and, in fact, expected.

like image 101
jox Avatar answered Nov 16 '22 16:11

jox


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.

like image 24
ptomato Avatar answered Nov 16 '22 15:11

ptomato