Pygtk: define key is a modifier

I have a key-press-event handler, and I need to determine what type of key was pressed: modifier or not?

This is not in event.state, because this field only works when the modifier was pressed with something else, but I need this for a single key (i.e. just by pressing the control button or alt, ...).

+6
python user-interface gtk pygtk
source share
2 answers

If your version of GTK + / PyGTK is fairly recent, key events have the is_modifier attribute. It is not documented in the PyGTK link, but it is contained in the GDK API documentation and displayed through PyGTK. It was added in GDK 2.10.

+4
source share

You will find what you are looking for in event.keyval. For example, the following code works for me:

 def key_press_event(widget, event): keyname = gtk.gdk.keyval_name(event.keyval) if "Control" in keyname or "Alt" in keyname: print "You pressed a modifier!" 
+2
source share

All Articles