GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 4:01 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Capture "Enter" key in a GtkCombo box preventing &
PostPosted: Thu Jan 11, 2007 6:22 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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:
/*
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 */
    }
}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 7:54 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
I'm not sure, but GtkWidget's can-activate-accel or mnemonic-activate signals may be of some help too.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 8:02 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group