Re: how can I emit a key-press-event?
- From: Juhana Sadeharju <kouhia nic funet fi>
- To: gtk-app-devel-list gnome org
- Subject: Re: how can I emit a key-press-event?
- Date: Fri, 2 Jul 2004 20:32:49 +0300
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.
http://tkomulai.iki.fi/synthesize-key-press-event.c
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.
Juhana
== 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,
event->group);
return FALSE;
}
int
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]