Re: GTK, keycodes and keysyms



Philip Kendall <pak ast cam ac uk> writes: 
Basically, a much more efficient version of what I eventually came up
with:

guint unshift_keysym(guint keysym)
{
  /* Oh boy is this ugly! */
  return XKeycodeToKeysym(gdk_display,
                          XKeysymToKeycode(gdk_display,keysym),
                          0);
}

Slow and ugly, but it works :-)


Heh, a good bit shorter too. ;-)

I think you were getting numbers? If you want to be totally Correct
(tm) then you might be interested in this code, from
gtk/gtkimcontextsimple.c, here we're letting the user enter hex digits
with Shift-Control held down, and of course want the digits, not
punctuation on the digit keys. This code handles the case where Latin
hex digits are entered in a level other than 0, e.g. on some
international keyboards.

In case you were curious why that keymap code was so
complicated... ;-)

Havoc


static gboolean
is_hex_keyval (guint keyval)
{
  gunichar ch = gdk_keyval_to_unicode (keyval);

  return g_unichar_isxdigit (ch);
}

static guint
canonical_hex_keyval (GdkEventKey *event)
{
  guint keyval;
  guint *keyvals = NULL;
  gint n_vals = 0;
  gint i;
  
  /* See if the keyval is already a hex digit */
  if (is_hex_keyval (event->keyval))
    return event->keyval;

  /* See if this key would have generated a hex keyval in
   * any other state, and return that hex keyval if so
   */
  gdk_keymap_get_entries_for_keycode (NULL,
                                      event->hardware_keycode, NULL,
                                      &keyvals, &n_vals);

  keyval = 0;
  i = 0;
  while (i < n_vals)
    {
      if (is_hex_keyval (keyvals[i]))
        {
          keyval = keyvals[i];
          break;
        }

      ++i;
    }

  g_free (keyvals);
  
  if (keyval)
    return keyval;
  else
    /* just return the keyval unchanged, we couldn't figure
     * out a way to make it a hex digit
     */
    return event->keyval;
}






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