/GTK 4.0

Overview of GTK input and event handling

This chapter describes in detail how GTK handles input. If you are interested in what happens to translate a key press or mouse motion of the users into a change of a GTK widget, you should read this chapter. This knowledge will also be useful if you decide to implement your own widgets.

The most basic input devices that every computer user has interacted with are keyboards and mice; beyond these, GTK supports touchpads, touchscreens and more exotic input devices such as graphics tablets. Inside GTK, every such input device is represented by a GdkDevice object.

To simplify dealing with the variability between these input devices, GTK has a concept of logical and physical devices. The concrete physical devices that have many different characteristics (mice may have 2 or 3 or 8 buttons, keyboards have different layouts and may or may not have a separate number block, etc) are represented as physical devices. Each physical device is associated with a virtual logical device. Logical devices always come in pointer/keyboard pairs - you can think of such a pair as a seat.

GTK widgets generally deal with the logical devices, and thus can be used with any pointing device or keyboard.

When a user interacts with an input device (e.g. moves a mouse or presses a key on the keyboard), GTK receives events from the windowing system. These are typically directed at a specific surface - for pointer events, the surface under the pointer (grabs complicate this), for keyboard events, the surface with the keyboard focus.

GDK translates these raw windowing system events into GdkEvents. Typical input events are button clicks, pointer motion, key presses or touch events. These are all represented as GdkEvents, but you can differentiate between different events by looking at their type, using gdk_event_get_event_type().

Some events, such as touch events or button press-release pairs, are connected in to each other in an event sequence that univocally identifies events that are related to the same interaction.

When GTK creates a GdkSurface, it connects to the “event” signal on it, which receives all of these input events. Surfaces have have signals and properties, e.g. to deal with window management related events.

© 2005–2020 The GNOME Project
Licensed under the GNU Lesser General Public License version 2.1 or later.