LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Connect Gtk's key event with button pressed? (https://www.linuxquestions.org/questions/programming-9/connect-gtks-key-event-with-button-pressed-267663/)

Chowroc 12-18-2004 04:56 AM

Connect Gtk's key event with button pressed?
 
I want to make connection with GTK's keyborad event and button event.

When I press a key, I want that a button in the window also has a status of being pressed, I have try to emit signals to do this, but has no effect:

Code:

#include <gtk/gtk.h>

gint key_press_cb(GtkWidget *widget, GdkEventKey *kevent, gpointer data)  {
    GtkWidget *btn = (GtkWidget *)data;
   
    if(kevent->type == GDK_KEY_PRESS)  {
        g_message("%d, %c;", kevent->keyval, kevent->keyval);
    }

    // void g_signal_emit_by_name(GObject *object, const gchar *name, ... );
    // g_signal_emit_by_name(G_OBJECT(btn), "clicked", NULL);
    g_signal_emit_by_name(G_OBJECT(widget), "clicked", NULL);

    return TRUE;
}

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

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 100, 50);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    button = gtk_button_new_with_label("OK");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_widget_show_all(window);

    // g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(key_press_cb), button);
    g_signal_connect(G_OBJECT(button), "key_press_event", G_CALLBACK(key_press_cb), button);
    gtk_main();
   
    return 0;
}

Is there any one can help me?

thanks.

clausi 12-18-2004 07:56 AM

I'm not absolutly sure if I understood the task you're trying to do (sorry, my english). For example, in your window is a buttom which should be activated when you press "Ctrl + A", right?

If so, you're looking for GtkToggleButton. There's a function gtk_toggle_button_set_active(), which may be called from your keyboard event to activate the buttom.

Hope this helps.

Chowroc 12-19-2004 12:49 AM

I does not mean shortcuts, for example, I want to write a screen keyboard, so the buttons must respond to the key events. When I press "k" on the real keyborad, one button "k" in the window must act just like been pressed.

Or write a digital calculator, when I press a number key, the button in the window must also "been pressed" at the same time.

thanks.

clausi 12-19-2004 04:59 PM

I see. I just checked the usual button and it looks as if pressed when you click it with your mouse.

So, what about using gtk_button_pressed()? Looks like an obvious choice to me.

For example, pressing "k" would call gtk_buttom_pressed() associated with the buttom, and releasing "k" would call gtk_buttom_released(). I think, this should model the effect, you want. An even more simple way seems to use gtk_buttom_clicked().

However, I'm not very familar with C, so consider this a suggestion.

Chowroc 12-20-2004 07:20 AM

No effect, and the document say it's also "emit signal".

I want to trace into this gtk functions with gdb, how can I do this? Or any other way?

thanks.

Chowroc 12-20-2004 09:45 AM

I read some source code of gtk, and achieve at last by this way:
Code:

g_signal_emit_by_name(G_OBJECT(button), "activate", NULL);

clausi 12-20-2004 01:52 PM

Cool. Please do us all a favour then, and post the complete code, instead of letting others figure out if you needed to change others parts, too.


All times are GMT -5. The time now is 07:48 PM.