gtk_accelerator_name & gtk_accelerator_parse not symmetrical



    In developing an application that allows users to define commands to
be called on specific keypresses, I've noticed that accelerators
involving the Shift key do not work. I've traced the problem until the
functions gtk_accelerator_name & gtk_accelerator_parse, that I use in my
program.

    The program below demonstrates this:

----8<----
/* Compile with gcc -Wall -g keycode.c -o keycode `pkg-config --cflags
--libs gtk+-2.0` */
#include <stdio.h>
#include <gtk/gtk.h>


static gboolean keypress_cb(GtkWidget *widget,GdkEventKey *evt, gpointer
data)
{
  if (gtk_accelerator_valid(evt->keyval, evt->state)) {
    guint            newkeyval;
    GdkModifierType  newmodifiers;
    gchar           *acc;

    acc = gtk_accelerator_name(evt->keyval, evt->state);
    printf("Key code: %s (keyval = 0x%x, mod = 0x%x)\n",
           acc, evt->keyval, evt->state);

    gtk_accelerator_parse(acc, &newkeyval, &newmodifiers);
    printf("Re-parsed: keyval = 0x%x, mod = 0x%x\n",
           newkeyval, newmodifiers);

  }
  return FALSE;
}


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

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect(G_OBJECT(window), "delete-event",
                   G_CALLBACK(gtk_main_quit), NULL);

  entry = gtk_entry_new();
  g_signal_connect(G_OBJECT(entry), "key-press-event",
                   G_CALLBACK(keypress_cb), NULL);

  gtk_container_add(GTK_CONTAINER(window), entry);
  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}
----8<----

If a key, such as "a" is pressed, one gets
Key code: a (keyval = 0x61, mod = 0x0)
Re-parsed: keyval = 0x61, mod = 0x0

However, if Shift-A is pressed, one gets
Key code: <Shift>a (keyval = 0x41, mod = 0x1)
Re-parsed: keyval = 0x61, mod = 0x1

Note that for Shift-A the keyval is different (GDK_A versus GDK_a), but
gtk_accelerator_name does not reflect that, and gtk_accelerator_parse
returns the virtually impossible combination "lowercase a with shift
pressed".

I suppose this is a bug. Still, is there some kind of workaround (except
by checking if the key is a letter and manually adjusting the codes?)

-- 
Pascal Users:
        To show respect for the 313th anniversary (tomorrow) of the
        death of Blaise Pascal, your programs will be run at half speed.

Eduardo M KALINOWSKI
ekalin gmail com
http://move.to/hpkb




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]