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
|
#include <gtk/gtk.h>
static void key_press_event (GtkWidget *window, GdkEventKey *gkey, GtkWidget *label);
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Key-pressed");
gtk_window_set_default_size (GTK_WINDOW(window), 200, 100);
g_signal_connect (G_OBJECT(window), "destroy", gtk_main_quit, NULL);
vbox = gtk_vbox_new (TRUE, 0);
gtk_container_add (GTK_CONTAINER(window), vbox);
label = gtk_label_new ("Example...");
gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
gtk_label_set_selectable (GTK_LABEL(label), TRUE);
g_signal_connect (GTK_OBJECT(window), "key-press-event", G_CALLBACK(key_press_event), (gpointer)label);
gtk_widget_show_all (window);
gtk_main ();
}
static void key_press_event (GtkWidget *window, GdkEventKey *gkey, GtkWidget *label)
{
if (gkey->type == GDK_KEY_PRESS)
{
g_print ("Key: %i\n", gkey->keyval);
if (gkey->keyval == 'a')
gtk_label_set_text (GTK_LABEL(label), "Pressed 'a'");
else if (gkey->keyval == 'b')
gtk_label_set_text (GTK_LABEL(label), "Pressed 'b");
else
gtk_label_set_text (GTK_LABEL(label), "Example...");
}
} |
Partager