Daniel
Daniel

Reputation: 4314

pygtk: determine key is a modifier

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, ...).

Upvotes: 6

Views: 1476

Answers (2)

Geoff Reedy
Geoff Reedy

Reputation: 36011

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.

Upvotes: 4

Daniel G
Daniel G

Reputation: 69692

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!"

Upvotes: 2

Related Questions