Event Propagation

As described in the appendix event signals are propagated in 3 phases:

  1. Capture phase - runs from the toplevel down to the event widget.

  2. Target phase - runs only on the event widget.

  3. Bubble phase - runs from the event widget up to the toplevel.

A keyboard event is first sent to the toplevel window (Gtk::Window), where it will be checked for any keyboard shortcuts that may be set (accelerator keys and mnemonics, used for selecting menu items from the keyboard). After this (and assuming the event wasn't handled), it is propagated down until it reaches the widget which has keyboard focus. The event will then propagate up until it reaches the top-level widget, or until you stop the propagation by returning true from an event handler.

Notice, that after canceling an event, no other function will be called (even if it is from the same widget).

Exempel

In this example there are 9 EventControllerKeys, 3 in each of a Gtk::Window, a Gtk::Box and a Gtk::Label. In each of the widgets there is one event controller for each propagation phase.

The purpose of this example is to show the steps the event takes when it is emitted.

When you write in the label, a key press event will be emitted, which will go first, in the capture phase, to the toplevel window (Gtk::Window), then down to its child, the box, then to the box's child, the label with the keyboard focus. In the target phase the event goes only to the widget with the keyboard focus (the label). In the bubble phase the event goes first to the widget with the keyboard focus (the label), then to its parent (the box), then to the box's parent (the window). If the event propagates all the way down to the label and then up to the window without being stopped, the text you're writing will appear in the Label above the Label you're writing in.

Keyboard Events - Event Propagation

Källkod