6

I've got key-press-event handler and i need to determine which kind of key was pressed: modifier or not?

It's not in event.state, because this field works only when modifier was pressed with something else, but i need this for single key (i.e. simply pressing control or alt, ...).

unwind
  • 391,730
  • 64
  • 469
  • 606
Daniel
  • 4,272
  • 8
  • 35
  • 48

2 Answers2

4

If your version of GTK+/PyGTK is recent enough, key events have a is_modifier attribute. It's not documented in the PyGTK reference, but it's in the GDK API documentation and is exposed through PyGTK. It was added in GDK 2.10.

unwind
  • 391,730
  • 64
  • 469
  • 606
Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79
2

You'll find what you're 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!"
Daniel G
  • 67,224
  • 7
  • 42
  • 42
  • i've got something like what you've shown, but much bigger: MODIFIERS=( gtk.keysyms.Control_L, ..... gtk.keysyms.Hyper_L, gtk.keysyms.Hyper_R, gtk.keysyms.Meta_L, gtk.keysyms.Meta_R, ) .... if event.keyval in MODIFIERS: .... but i thought there could be some another approach – Daniel Jan 27 '10 at 21:48