Return to Snippet

Revision: 50066
at August 6, 2011 17:16 by FArcellier


Updated Code
/*! 
 * \brief GTK Program to get information about key pressed
 * \author Fabien Arcellier
 * 
 * To compil with GTK : gcc -Wall `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o key_tool -g key_tool.c
 */
#include <gtk/gtk.h>

static void destroy (GtkWidget*, gpointer);
static gboolean key_press_event (GtkWidget*, GdkEventKey*, gpointer);

int main(int argc, char *argv[])
{
  GtkWidget *window = NULL, *label= NULL;
  
  gtk_init(&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Key Pressed tool");
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  gtk_widget_set_size_request (window, 300, 300);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  
  label = gtk_label_new ("Press a key");
  gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  
  gtk_container_add (GTK_CONTAINER (window), label);
  gtk_widget_show_all (window);
  
  g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (key_press_event), NULL);
  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
  
  gtk_main();
  return 0;
}

/*!
 * \brief Close application
 */
static void destroy (GtkWidget *window, gpointer data) 
{
  gtk_main_quit();
}

/*!
 * \brief Display information about key pressed in label
 */
static gboolean key_press_event(GtkWidget *window, GdkEventKey *event, gpointer data)
{
  char key_event_info[1024] = "";
  char *event_string = NULL;
  GtkWidget *label = NULL;
  
  if (event -> string != NULL)
  {
    event_string = event -> string;
  }
  else
  {
    *event_string = '\0';
  }
  
  sprintf(key_event_info, "Key\n\n \
    send_event : %d\n \
    time : %d \n \
    state : %d \n \
    keyval : %d \n \
    length : %d \n \
    string : %s \n \
    hardware_keycode : %d \n \
    group : %d \n \
    is_modifier %d",
    event -> send_event,
    event -> time,
    event -> state,
    event -> keyval,
    event -> length,
    event_string,
    event -> hardware_keycode,
    event -> group,
    event -> is_modifier
  );
  
  label = gtk_bin_get_child (GTK_BIN (window));
  gtk_label_set_text (GTK_LABEL (label), key_event_info);
  
  return TRUE;
}

Revision: 50065
at August 6, 2011 17:15 by FArcellier


Initial Code
/*!
    * \brief GTK Program to get information about key pressed
    * \author Fabien Arcellier
    *
    * To compil with GTK : gcc -Wall `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o key_tool -g key_tool.c
    */
    #include <gtk/gtk.h>
    static void destroy (GtkWidget*, gpointer);
    static gboolean key_press_event (GtkWidget*, GdkEventKey*, gpointer);
    int main(int argc, char *argv[])
    {
    GtkWidget *window = NULL, *label= NULL;
    gtk_init(&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Key Pressed tool");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request (window, 300, 300);
    gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
    label = gtk_label_new ("Press a key");
    gtk_label_set_selectable (GTK_LABEL (label), TRUE);
    gtk_container_add (GTK_CONTAINER (window), label);
    gtk_widget_show_all (window);
    g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (key_press_event), NULL);
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
    gtk_main();
    return 0;
    }
    /*!
    * \brief Close application
    */
    static void destroy (GtkWidget *window, gpointer data)
    {
    gtk_main_quit();
    }
    /*!
    * \brief Display information about key pressed in label
    */
    static gboolean key_press_event(GtkWidget *window, GdkEventKey *event, gpointer data)
    {
    char key_event_info[1024] = "";
    char *event_string = NULL;
    GtkWidget *label = NULL;
    if (event -> string != NULL)
    {
    event_string = event -> string;
    }
    else
    {
    *event_string = '\0';
    }
    sprintf(key_event_info, "Key\n\n \
    send_event : %d\n \
    time : %d \n \
    state : %d \n \
    keyval : %d \n \
    length : %d \n \
    string : %s \n \
    hardware_keycode : %d \n \
    group : %d \n \
    is_modifier %d",
    event -> send_event,
    event -> time,
    event -> state,
    event -> keyval,
    event -> length,
    event_string,
    event -> hardware_keycode,
    event -> group,
    event -> is_modifier
    );
    label = gtk_bin_get_child (GTK_BIN (window));
    gtk_label_set_text (GTK_LABEL (label), key_event_info);
    return TRUE;
    }

Initial URL

                                

Initial Description

                                

Initial Title
Get Key information with GTK

Initial Tags
c

Initial Language
C