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 Dec 19, 2014 5:31 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Callback function when a key is pressed
PostPosted: Tue Mar 27, 2012 10:35 pm 
Offline
Familiar Face

Joined: Tue Mar 27, 2012 7:51 pm
Posts: 6
On pygtk there's the possibility to call a callback function when a button is pressed.
Is there a way to make a similar thing if a keyboard key is pressed?

Thanks in advance,
FrankBr


Top
 Profile  
 
 Post subject: Re: Callback function when a key is pressed
PostPosted: Wed Mar 28, 2012 6:31 am 
Offline
Familiar Face

Joined: Thu Dec 15, 2011 6:23 pm
Posts: 36
Location: Hrvatska
I don't work with python, only knows how GTK works on C
GtkWidget class has key-press-event and key-release-event signal
callback is well described in documentation.
on fedora 16 there is nice application DEVHELP for GTK documentation.
hope you will find something similar for python.
good luck !

EDIT: now I see, you spread one question on several post, you allready found answer for this :)


Top
 Profile  
 
 Post subject: Re: Callback function when a key is pressed
PostPosted: Wed Mar 28, 2012 12:06 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Yes you can. In fact it is very much advisable (part of the ATK guidelines).
How you go about doing it will depend on what you want:
1. make the widget default. The callback is made with "Enter"
2. You can set a mnemonic on the button. The callback is made with Alt+mnemonic key. Probably the best option for most cases.
3. Add an accelerator. Most general case -- can do anything, but does not display the funky underscore thing when you press Alt like 2 to indicate its presence.


Top
 Profile  
 
 Post subject: Re: Callback function when a key is pressed
PostPosted: Wed Mar 28, 2012 12:54 pm 
Offline
Familiar Face

Joined: Tue Mar 27, 2012 7:51 pm
Posts: 6
Ok, I suppose I wanna use your second option.
How to perform it?
Could you help me?


Top
 Profile  
 
 Post subject: Re: Callback function when a key is pressed
PostPosted: Wed Mar 28, 2012 10:52 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
check the documentation http://developer.gnome.org/gtk/2.24/GtkButton.html#gtk-button-new-with-mnemonic. It's easy: e.g. gtk_button_new_with_mnemonic("_Button") will trigger Alt+B.
There's most likely an equivalent method in PyGTK.


Top
 Profile  
 
 Post subject: [Solved] Re: Callback function when a key is pressed
PostPosted: Fri Mar 30, 2012 11:11 am 
Offline
Familiar Face

Joined: Tue Mar 27, 2012 7:51 pm
Posts: 6
I worked out with this:
Code:
   

self.window.connect('key_press_event', self.on_key_press_event)

def on_key_press_event(self, w, event):
       keyname = gtk.gdk.keyval_name(event.keyval)
        do something..


where "keyname" returns me what I've pressed.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group