GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Capture "Enter" key in a GtkCombo box preventing &

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 428
Location: Portland, OR USA

PostPosted: Thu Jan 11, 2007 6:22 pm    Post subject: Capture "Enter" key in a GtkCombo box preventing & Reply with quote

Somebody asked in gtk-list how to "capture" the Enter key being pressed in a GtkComboBox rather than the default behavior in which the combo box simply "drops down" when the enter key is pressed. I wrote this little ditty to test it out... it uses the GtkWindow "key-press-event" to capture the enter key being pressed and if it IS the enter key, I print the value in the GtkComboBox to the screen.

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Compile with:
gcc -Wall -g -o combo_enter `pkg-config --cflags --libs gtk+-2.0` combo_enter.c

Note: gdk/gdkkeysyms is not included in gtk.h. It contains the keycodes we need
      to capture the "return" key being pressed.
*/
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

/* prototypes */
static void destroy (GtkWidget*, gpointer);
gboolean on_combo_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data);

int main (int argc, char *argv[])
{
    GtkWidget       *window = NULL;
    GtkWidget       *combo = NULL;
   
    gtk_init (&argc, &argv);

    /* setup GtkWindow */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (window, 150, 30);
    g_signal_connect (G_OBJECT (window), "destroy",
            G_CALLBACK (destroy), NULL);
 
    /* setup a test GtkComboBox */
    combo = gtk_combo_box_new_text ();
    gtk_combo_box_append_text (GTK_COMBO_BOX(combo), "test 1");
    gtk_combo_box_append_text (GTK_COMBO_BOX(combo), "test 2");
    g_signal_connect (G_OBJECT (combo), "key-press-event",
            G_CALLBACK (on_combo_key_press), NULL);
               
    gtk_container_add (GTK_CONTAINER (window), combo);
 
    /* show window */
    gtk_widget_show_all (window);
    gtk_main ();
   
    return 0;
}

static void
destroy (GtkWidget *window, gpointer data)
{
    gtk_main_quit ();
}

gboolean
on_combo_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
    /* user has pressed a key on/in the GtkComboBox */
   
    if (event->keyval == GDK_Return)
    {
        /* user pressed the enter key */
        gchar *str = gtk_combo_box_get_active_text (GTK_COMBO_BOX(widget));
        g_print ("Value: %s\n", str);
        g_free (str);
       
        return TRUE;    /* don't let the box "drop down" */
    }
    else
    {
        return FALSE; /* propogate event */
    }
}
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Thu Jan 11, 2007 7:54 pm    Post subject: Reply with quote

I'm not sure, but GtkWidget's can-activate-accel or mnemonic-activate signals may be of some help too.
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 428
Location: Portland, OR USA

PostPosted: Thu Jan 11, 2007 8:02 pm    Post subject: Reply with quote

Good point.

The question in the gtk-list actually wanted to capture the event to "send a message" to the user specified in the combo box in an IM-like application... which could be done where I have it printing the value to the screen.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP