GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 29, 2014 7:18 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Map Button and function key to each other (e.g. F1 <-> Help)
PostPosted: Sat Dec 31, 2011 2:52 pm 
Offline

Joined: Sat Dec 31, 2011 2:31 pm
Posts: 4
Hi all,

can anybody point me to a documentation or give an answer to the following problem:

- my application provides help for most of the widgets on a window. Help shal be provided by pressing the 'F1' function key.
- on the bottom of the window I provide a number of standard buttons, such as OK, Cancel and so on. A 'Help' button shall be provided as well.

For the button, I just use (it's all in perl)

Code:
$button->signal_connect('clicked' => $helpFunc, $dtaPtr)


which works fine.

My questions:
1. How can I achieve that pressing 'F1' triggers the same behaviour (namely a popup window with some help text) ?
2. If 'F1' was used instead of the 'Help' button: How can I optically 'simulate' that a 'Help' button was clicked? (accelerator ..?)

What I would like to have is a guideline, in C, C++ or perl, any of these is allright. To find out the particular equivalent perl calls is not a big issue (I hope :-o).

Your help is very much appreciated.

Greetings,
emax


Top
 Profile  
 
 Post subject: Re: Map Button and function key to each other (e.g. F1 <-> H
PostPosted: Sat Dec 31, 2011 9:32 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Your are looking for GtkAccelGroup:

In C (gtk+-2.0):

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

void on_help(GtkWidget *widget, gpointer data)
{
    g_print("Help!\n");
}

int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    GtkWidget *button=gtk_button_new_with_label("Help");
    gtk_container_add(GTK_CONTAINER(window),button);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_help),NULL);

    GtkAccelGroup *accel_group=gtk_accel_group_new();
    gtk_window_add_accel_group(GTK_WINDOW(window),accel_group);
    gtk_widget_add_accelerator(button,"clicked",accel_group,GDK_KEY_F1,0,0);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}


Top
 Profile  
 
 Post subject: Re: Map Button and function key to each other (e.g. F1 <-> H
PostPosted: Sun Jan 01, 2012 6:32 pm 
Offline

Joined: Sat Dec 31, 2011 2:31 pm
Posts: 4
I will try this in perl.

Thanks a lot! :-)


Top
 Profile  
 
 Post subject: Re: Map Button and function key to each other (e.g. F1 <-> H
PostPosted: Tue Jan 03, 2012 3:06 pm 
Offline

Joined: Sat Dec 31, 2011 2:31 pm
Posts: 4
The example worked fine in C, execpt for the GDK_KEY_F1 which is a GDK_F1 in my installation.

Thank you again.

But with the perl-bindings, the "gtk_widget_add_accelerator(button,"clicked",accel_group,GDK_KEY_F1,0,0);" equivalent doesn't accept zeros for the mask and the modifier parameter. I have to provide a value, which is a problem as I do not want to use a modifier (such as CTRL).

I think, I will go with key_press_event and check for such keypresses 'manually'.


Top
 Profile  
 
 Post subject: Re: Map Button and function key to each other (e.g. F1 <-> H
PostPosted: Tue Jan 03, 2012 4:18 pm 
Offline

Joined: Sat Dec 31, 2011 2:31 pm
Posts: 4
Update:

In perl, things differ a little bit, and I finally could add an accelerator. The 'zero' values are done differently as well, so to whom it may concern - here is the result:

Code:
    # f1:Help button
    my $button = Gtk2::Button->new('F1: Help');
    $button->signal_connect('clicked' => \&dummyHelp);
    my $accelGroup = Gtk2::AccelGroup->new;
    $accelGroup->connect($Gtk2::Gdk::Keysyms{F1}, [], [], \&dummyHelp);
    $window->add_accel_group($accelGroup);


So there is no add_accelerator, instead, a connect call has to be done.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group