LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2004, 04:56 AM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
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.
 
Old 12-18-2004, 07:56 AM   #2
clausi
Member
 
Registered: Mar 2004
Distribution: Debian
Posts: 48

Rep: Reputation: 15
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.
 
Old 12-19-2004, 12:49 AM   #3
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
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.
 
Old 12-19-2004, 04:59 PM   #4
clausi
Member
 
Registered: Mar 2004
Distribution: Debian
Posts: 48

Rep: Reputation: 15
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.
 
Old 12-20-2004, 07:20 AM   #5
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
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.
 
Old 12-20-2004, 09:45 AM   #6
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
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);
 
Old 12-20-2004, 01:52 PM   #7
clausi
Member
 
Registered: Mar 2004
Distribution: Debian
Posts: 48

Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RHEL 3 - Keyboard send several chars when key pressed once lmx Red Hat 1 01-29-2005 11:46 PM
is certain key pressed at the moment ?? freeborn Programming 4 11-03-2004 03:13 AM
Unknown key pressed Khang Linux - Newbie 3 10-27-2004 04:12 PM
I am getting a ^H when the backspace key is pressed. The only time that it happens is mlawler Linux - Networking 2 04-29-2004 02:37 AM
Automatic shutdown when power button pressed.. mr_smidge Linux - General 9 09-08-2003 02:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration