Re: emit keypress event into event loop



On Fri, Sep 21, 2007 at 03:32:49PM -0100, vano wrote:

Anybody has idea how to emit in GTK (2.11.6) some keypress event into
main event loop?

I have mouse clickable button and want to send to my text_view a
keyboard press.

I'm doing this way:

in main code:

g_signal_connect(G_OBJECT(my_button), "button-press-event",
         G_CALLBACK(press_callback), G_OBJECT(textview));

Why on Earth you are connecting to "button-press-event"
instead of "clicked" of the button?  (And with a handler that
has the wrong prototype -- event signals have a boolean return
value.)

in callback:

static void press_callback( GtkWidget *widget,
                            GdkEventButton *e, GtkWidget *text_view )
{
        g_print("button press\n");

        GdkEvent *event;

        event = gdk_event_new(GDK_KEY_PRESS);

        event->key.keyval = GDK_K;
        event->key.window = gtk_text_view_get_window(text_view,
GTK_TEXT_WINDOW_TEXT);
        event->key.time = 0;

        gtk_main_do_event(&event);

If your compiler does not complain loudly on this line,
enable some warnings, it will save you lots of troubles.
(And if it does, read what is says before asking in the
list...)

You pass the wrong type to gtk_main_do_event() (one level of
pointerness too many).

or if trying another approach with sending event directly to text_view
widget:

static void press_callback( GtkWidget *widget,
                            GdkEventButton *e, GtkWidget *text_view )
{
        g_print("button press\n");

        GdkEvent *event;

        event = gdk_event_new(GDK_KEY_PRESS);

        event->key.keyval = GDK_K;
        event->key.window = gtk_text_view_get_window(text_view,
GTK_TEXT_WINDOW_TEXT);
        event->key.time = 0;

      g_signal_emit_by_name(GTK_TEXT_VIEW(text_view), "key_press_event", event);

}

the program seems go in correct way, but I get segmentation fault:

First, event signals cannot be sent this simple way as other
signals.

Anyway, event signals have boolean return value and you do
not pass any return location (see g_signal_emit_by_name()
documentation), therefore it overwrites some stuff on the
stack that follows the arguments.

Now something constructive: Synthetizing events is almost
always a bad idea.  At the end, you want the button to
behave normally (i.e. connect to "clicked") and perform some
specific action: insert text, delete text, change text, ...
For all these things GtkTextView/Buffer have methods.  Use
them directly.

Yeti

--
http://gwyddion.net/



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