Re: how can I emit a key-press-event?

From: Tommi Komulainen <tommi komulainen nokia com>

Hmm..  The attachment really was there when I sent this.  You can find
the file from the following address as well.

Just include it to your message, like I have included the code
below. Short good examples are better if they are in the mail archives
and not as dead urls later.


 == cut ==

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

static gboolean
on_timeout (gpointer user_data)
  GtkWidget *widget = user_data;
  GdkKeymapKey *keys;
  gint n_keys;

  if (gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (),
                                         GDK_o, &keys, &n_keys))
      guint16 hardware_keycode;
      GdkEvent *event;

      /* we can pick any hardware_keycode that generates GDK_o */
      hardware_keycode = keys[0].keycode;
      g_free (keys);

      /* synthesize Alt+O key press */
      event = gdk_event_new (GDK_KEY_PRESS);
      event->key.window = widget->window;
      event->key.state = GDK_MOD1_MASK;               /* Alt */
      event->key.hardware_keycode = hardware_keycode; /*  +O */

      /* these are optional but possibly nice to have */
      event->key.keyval = gdk_unicode_to_keyval (GDK_o);
      event->key.length = 1;
      event->key.string = g_strdup ("o");

      /* the rest are no-ops */
      event->key.send_event = FALSE;      /* seems unused except for dnd */
      event->key.time = GDK_CURRENT_TIME;   

      gtk_main_do_event (event);
      gdk_event_free (event);

  return FALSE;

static gboolean
on_key_press_event (GtkWidget *widget, GdkEventKey *event)
  g_print ("send_event=%d, state=%u, keyval=%u, length=%d, string='%s', hardware_keycode=%u, group=%u\n", 
event->send_event, event->state, event->keyval, event->length, event->string, event->hardware_keycode, 
  return FALSE;

main (int argc, char **argv)
  GtkWidget *win;

  gtk_init (&argc, &argv);

  win = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_QUESTION,
                                GTK_BUTTONS_OK_CANCEL, "OK?");
  g_signal_connect (win, "key-press-event", G_CALLBACK(on_key_press_event), 0);
  g_timeout_add (2500, on_timeout, win);
  gtk_dialog_run (GTK_DIALOG(win));

  return 0;

 == end ==

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