GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 24, 2014 12:48 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Button to grab a shortcut
PostPosted: Wed Sep 12, 2012 2:24 pm 
Offline

Joined: Wed Sep 12, 2012 2:05 pm
Posts: 2
Hello everyone,

I am developing a MUD client and was wondering if there is a special button for grabbing shortcuts. Some applications have a button which awaits for an event upon press.

I believe I could use a button and an entry which shows then the pressed event.

You might be wondering for what that is good. In MUD, you have to input a lot of commands. Sometimes, players want to avoid having to type enter, so they create special events for keys like arrows that upon press do something configurable.

I know I can grab all key presses from a window by connecting the key-press-event signal to a callback. Then, I am able to compare the event with any stored event and execute a corresponding command. But I also have to allow the user to store events and commands.
My question is how to efficiently grab an event and store it.

I hope someone might know what I mean. An example is in the system configuration of shortcuts for desktop environments. Generally, you can input the event and it shows ctrl+1 instead of: <gtk.gdk.Event at 0x1512d28: GDK_KEY_PRESS keyval=Control_1>. Even if I take the keyval of the object, it is an integer.

I thank you in advance,
Raul


Top
 Profile  
 
 Post subject: Re: Button to grab a shortcut
PostPosted: Wed Sep 12, 2012 11:08 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
GTK is built upon ATK so providing accessibility is a key part of the toolkit and all apps should be designed so that users with limited mobility can use it effectively, e.g. a mouse should not be required. In your case this is good as gtk automatically has the ability to tab between buttons built in and you can add key-bindings to buttons to make this even easier. Unfortunately guidelines for using ATK in GTK is nonexistent in gtk tutorials.
I assume that for whatever key press event you might want that you will also have a button somewhere on the display that will do the same thing. In this case you just add an accelerator to each button. E.g. the following binds control p to a button:
Code:
GtkAccelGroup *accel_group=NULL;
...
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_p, GDK_CONTROL_MASK, NULL);


Top
 Profile  
 
 Post subject: Re: Button to grab a shortcut
PostPosted: Tue Sep 18, 2012 1:04 pm 
Offline

Joined: Wed Sep 12, 2012 2:05 pm
Posts: 2
Hello Paul,

thanks for your input. I was actually looking for a way to dynamically add shortcuts to my application whose action is defined by the user too.

This is pretty much what the keyboard part of gnome-control-center does. Looking into their code, I found out that the widget to grab a shortcut is the CellRendererAccel.

Using it, you don't have to tweak much in order to have your shortcut set. You only have to connect "accel-edited" to a callback and set the accel-key and accel-mods properties in the callback.

Using the AccelGroup's connect_group, the assigned callback is called for every registered key events. Parsing the keyval and keymods through the list of registered accelerators, I could perform the action defined by the user along with the accelerator.

I hope that helps someone looking for this same thing.
Best regards,
Raul


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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