Cannot catch key-press event, even with EventBox

I’m implement a GUI like this:

where I want to let user add image by copy/paste from other program.

I thought that, to do that, I first need to catch the Ctrl+V key press event. The problem is that, even though I add the Gtk.EventBox, set event mask for it and connect “key-press-event” signal, the call function still doesn’t get called when I type.

Here is my source code:

Did you call Gtk.Widget.add_events() with the key mask, e.g.

  event_mask = Gdk.EventMask.KEY_PRESS_MASK |
               Gdk.EventMask.KEY_RELEASE_MASK
  widget.add_events(event_mask)

The key-press-event and key-release-event signals are not emitted unless the event mask includes those values.

I set the mask in Glade, for the EventBox, but the signal is still not emitted:

Even if I add in code, still no signal is emitted

Can you try to set the can-focus property on the event box to True, and then call Gtk.Widget.grab_focus():

event_box.set_can_focus(True)

def on_mapped(window):
    event_box.grab_focus()

window.connect('mapped', on_mapped)

Key events are delivered only to the widgets that have key focus.

Thank you. It works. But I need to change a bit:

  • Call event_box.grab_focus() at other event (when stack is switched), because if I call at “window mapped”, the app menu is not built (don’t know why, no error message is shown).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.