#include <gtk/gtk.h>

static gboolean key_event(GtkWidget *widget, GdkEventKey *event, GtkLabel *label)
{
    g_printerr("key: %s, keycode: %d\n", gdk_keyval_name (event->keyval), event->keyval);

    GdkKeymapKey *keys;
    gint nkeys;
    if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), event->keyval, &keys, &nkeys))
        g_printerr("-> hardware_keycode: %d, group: %d, level: %d\n", keys[0].keycode, keys[0].group, keys[0].level);
    else
        g_printerr("-> error\n");
    g_free(keys);

    guint keyval;
    gint group,level;
    gdk_keymap_translate_keyboard_state(
            gdk_keymap_get_for_display(gdk_display_get_default()),
            event->keyval,
            (GdkModifierType)event->state,
            event->group,
            &keyval,
            &group,
            &level,
            NULL);
    g_printerr("-> hardware_keycode: %d, group: %d, level: %d\n", keyval, group, level);

    gchar *accel_label = gtk_accelerator_get_label(event->keyval, (GdkModifierType)event->state);
    g_printerr("-> accel label: %s\n", accel_label);
    gtk_label_set_text (label, accel_label);
    

    return FALSE;
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *label;

    gtk_init(&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    label = gtk_label_new("text");
    gtk_container_add(GTK_CONTAINER(window), label);

    g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), label);
    g_signal_connect(G_OBJECT(window), "destroy",  G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show (label);
    gtk_widget_show (window);
    gtk_main ();

    return 0;
}
