# Xkb Events

The primary way the X server communicates with clients is by sending X events to them. Some events are sent to all clients, while others are sent only to clients that have requested them. Some of the events that can be requested are associated with a particular window and are only sent to those clients who have both requested the event and specified the window in which the event occurred.

The Xkb extension uses events to communicate the keyboard status to interested clients. These events are not associated with a particular window. Instead, all Xkb keyboard status events are reported to all interested clients, regardless of which window currently has the keyboard focus and regardless of the grab state of the keyboard.

The one exception to this rule is the XkbExtensionDeviceNotify event report that is sent when a client attempts to use an unsupported feature of an X Input Extension device (see section 21.4).

The X server reports the events defined by the Xkb extension to your client application only if you have requested them. You may request Xkb events by calling either `XkbSelectEvents` or `XkbSelectEventDetails`. `XkbSelectEvents` requests Xkb events by their event type and causes them to be reported to your client application under all circumstances. You can specify a finer granularity for event reporting by using `XkbSelectEventDetails`; in this case events are reported only when the specific detail conditions you specify have been met.

## Xkb Event Types

The Xkb Extension adds new event types to the X protocol definition. An Xkb event type is defined by two fields in the X event data structure. One is the type field, containing the base event code. This base event code is a value the X server assigns to each X extension at runtime and that identifies the extension that generated the event; thus, the event code in the type field identifies the event as an Xkb extension event, rather than an event from another extension or a core X protocol event. You can obtain the base event code via a call to `XkbQueryExtension` or `XkbOpenDisplay`. The second field is the Xkb event type, which contains a value uniquely identifying each different Xkb event type. Possible values are defined by constants declared in the header file `<X11/extensions/Xkb.h>`.

Table 4.1 lists the categories of events defined by Xkb and their associated event types, as defined in `Xkb.h`. Each event is described in more detail in the section referenced for that event.

Xkb Event Types

Event Type | Conditions Generating Event | Section
--- | --- | ---
XkbNewKeyboardNotify | Keyboard geometry; keycode range change | 19
XkbMapNotify | Keyboard mapping change | 14.4
XkbStateNotify | Keyboard state change | 5.4
XkbControlsNotify | Keyboard controls state change | 10.11
XkbIndicatorStateNotify | Keyboard indicators state change | 8.5
XkbIndicatorMapNotify | Keyboard indicators map change | 8.5
XkbNamesNotify | Keyboard name change | 18.5
XkbCompatMapNotify | Keyboard compatibility map change | 17.5
XkbBellNotify | Keyboard bell generated | 9.4
XkbActionMessage | Keyboard action message | 16.1.11
XkbAccessXNotify | AccessX state change | 10.6.4
XkbExtensionDeviceNotify | Extension device change | 21.6

## Xkb Event Data Structures

Xkb reports each event it generates in a unique structure holding the data values needed to describe the conditions the event is reporting. However, all Xkb events have certain things in common. These common features are contained in the same fields at the beginning of all Xkb event structures and are described in the XkbAnyEvent structure:

    typedef struct {
        int             type;        /* Xkb extension base event code */
        unsigned long   serial;      /* X server serial number for event */
        Bool            send_event;  /* True ⇒ synthetically generated */
        Display *       display;     /* server connection where event generated */
        Time            time;        /* server time when event generated */
        int             xkb_type;    /* Xkb minor event code */
        unsigned int    device;      /* Xkb device ID, will not be XkbUseCoreKbd */
    } XkbAnyEvent;

events XkbAnyEvent
XkbAnyEvent

For any Xkb event, the type field is set to the base event code for the Xkb extension, assigned by the server to all Xkb extension events. The serial, send_event, and display fields are as described for all X11 events. The time field is set to the time when the event was generated and is expressed in milliseconds. The xkb_type field contains the minor extension event code, which is the extension event type, and is one of the values listed in Table 4.1. The device field contains the keyboard device identifier associated with the event. This is never XkbUseCoreKbd, even if the request that generated the event specified a device of XkbUseCoreKbd. If the request that generated the event specified XkbUseCoreKbd, device contains a value assigned by the server to specify the core keyboard. If the request that generated the event specified an X input extension device, device contains that same identifier.

Other data fields specific to individual Xkb events are described in subsequent chapters where the events are described.

## Selecting Xkb Events

Xkb events are selected using an event mask, much the same as normal core X events are selected. However, unlike selecting core X events, where you must specify the selection status (on or off) for all possible event types whenever you wish to change the selection criteria for any one event, Xkb allows you to restrict the specification to only the event types you wish to change. This means that you do not need to remember the event selection values for all possible types each time you want to change one of them.

Many Xkb event types are generated under several different circumstances. When selecting to receive an Xkb event, you may specify either that you want it delivered under all circumstances, or that you want it delivered only for a subset of the possible circumstances.

You can also deselect an event type that was previously selected for, using the same granularity.

Xkb provides two functions to select and deselect delivery of Xkb events. `XkbSelectEvents` allows you to select or deselect delivery of more than one Xkb event type at once. Events selected using `XkbSelectEvents` are delivered to your program under all circumstances that generate the events. To restrict delivery of an event to a subset of the conditions under which it occurs, use `XkbSelectEventDetails`. `XkbSelectEventDetails` only allows you to change the selection conditions for a single event at a time, but it provides a means of fine-tuning the conditions under which the event is delivered.

To select and / or deselect for delivery of one or more Xkb events and have them delivered under all conditions, use `XkbSelectEvents`.

Bool `XkbSelectEvents` Display *display unsigned int device_spec unsigned long int bits_to_change unsigned long int values_for_bits

display

  connection to the X server

device_spec

  device ID, or XkbUseCoreKbd

bits_to_change

  determines events to be selected / deselected

values_for_bits

  1⇒select, 0→deselect; for events in bits_to_change

This request changes the Xkb event selection mask for the keyboard specified by device_spec.

Each Xkb event that can be selected is represented by a bit in the bits_to_change and values_for_bits masks. Only the event selection bits specified by the bits_to_change parameter are affected; any unspecified bits are left unchanged. To turn on event selection for an event, set the bit for the event in the bits_to_change parameter and set the corresponding bit in the values_for_bits parameter. To turn off event selection for an event, set the bit for the event in the bits_to_change parameter and do not set the corresponding bit in the values_for_bits parameter. The valid values for both of these parameters are an inclusive bitwise OR of the masks shown in Table 4.2. There is no interface to return your client’s current event selection mask. Clients cannot set other clients’ event selection masks.

If a bit is not set in the bits_to_change parameter, but the corresponding bit is set in the values_for_bits parameter, a BadMatch protocol error results. If an undefined bit is set in either the bits_to_change or the values_for_bits parameter, a BadValue protocol error results.

All event selection bits are initially zero for clients using the Xkb extension. Once you set some bits, they remain set for your client until you clear them via another call to `XkbSelectEvents`.

`XkbSelectEvents` returns False if the Xkb extension has not been initialized and True otherwise.

To select or deselect for a specific Xkb event and optionally place conditions on when events of that type are reported to your client, use `XkbSelectEventDetails`. This allows you to exercise a finer granularity of control over delivery of Xkb events with `XkbSelectEvents`.

Bool `XkbSelectEventDetails` Display *display unsigned int device_spec unsigned int event_type unsigned long int bits_to_change unsigned long int values_for_bits

display

  connection to the X server

device_spec

  device ID, or XkbUseCoreKbd

event_type

  Xkb event type of interest

bits_to_change

  event selection details

values_for_bits

  values for bits selected by bits_to_change

While `XkbSelectEvents` allows multiple events to be selected, `XkbSelectEventDetails` changes the selection criteria for a single type of Xkb event. The interpretation of the bits_to_change and values_for_bits masks depends on the event type in question.

`XkbSelectEventDetails` changes the Xkb event selection mask for the keyboard specified by device_spec and the Xkb event specified by event_type. To turn on event selection for an event detail, set the bit for the detail in the bits_to_change parameter and set the corresponding bit in the values_for_bits parameter. To turn off event detail selection for a detail, set the bit for the detail in the bits_to_change parameter and do not set the corresponding bit in the values_for_bits parameter.

If an invalid event type is specified, a BadValue protocol error results. If a bit is not set in the bits_to_change parameter, but the corresponding bit is set in the values_for_bits parameter, a BadMatch protocol error results. If an undefined bit is set in either the bits_to_change or the values_for_bits parameter, a BadValue protocol error results.

For each type of Xkb event, the legal event details that you can specify in the `XkbSelectEventDetails` request are listed in the chapters that describe each event in detail.

### Event Masks

The X server reports the events defined by Xkb to your client application only if you have requested them via a call to `XkbSelectEvents` or `XkbSelectEventDetails`. Specify the event types in which you are interested in a mask, as described in section 4.3.

Table 4.2 lists the event mask constants that can be specified with the `XkbSelectEvents` request and the circumstances in which the mask should be specified.

XkbSelectEvents Mask Constants

Event Mask | Value | Notification Wanted
--- | --- | ---
XkbNewKeyboardNotifyMask | (1L<<0) | Keyboard geometry change
XkbMapNotifyMask | (1L<<1) | Keyboard mapping change
XkbStateNotifyMask | (1L<<2) | Keyboard state change
XkbControlsNotifyMask | (1L<<3) | Keyboard control change
XkbIndicatorStateNotifyMask | (1L<<4) | Keyboard indicator state change
XkbIndicatorMapNotifyMask | (1L<<5) | Keyboard indicator map change
XkbNamesNotifyMask | (1L<<6) | Keyboard name change
XkbCompatMapNotifyMask | (1L<<7) | Keyboard compat map change
XkbBellNotifyMask | (1L<<8) | Bell
XkbActionMessageMask | (1L<<9) | Action message
XkbAccessXNotifyMask | (1L<<10) | AccessX features
XkbExtensionDeviceNotifyMask | (1L<<11) | Extension device
XkbAllEventsMask | (0xFFF) | All Xkb events

events mask
mask event
XkbSelectEvents
XkbSelectEventDetails

## Unified Xkb Event Type

The XkbEvent structure is a union of the individual structures declared for each Xkb event type and for the core protocol XEvent type. Given an XkbEvent structure, you may use the type field to determine if the event is an Xkb event (type equals the Xkb base event code; see section 2.4). If the event is an Xkb event, you may then use the any.xkb_type field to determine the type of Xkb event and thereafter access the event-dependent components using the union member corresponding to the particular Xkb event type.

    typedef union _XkbEvent {
          int                            type;
          XkbAnyEvent                    any;
          XkbStateNotifyEvent            state;
          XkbMapNotifyEvent              map;
          XkbControlsNotifyEvent         ctrls;
          XkbIndicatorNotifyEvent        indicators;
          XkbBellNotifyEvent             bell;
          XkbAccessXNotifyEvent          accessx;
          XkbNamesNotifyEvent            names;
          XkbCompatMapNotifyEvent        compat;
          XkbActionMessageEvent          message;
          XkbExtensionDeviceNotifyEvent  device;
          XkbNewKeyboardNotifyEvent      new_kbd;
          XEvent                         core;
    } XkbEvent;

This unified Xkb event type includes a normal XEvent as used by the core protocol, so it is straightforward for applications that use Xkb events to call the X library event functions without having to cast every reference. For example, to get the next event, you can simply declare a variable of type XkbEvent and call:

    XNextEvent(dpy,&xkbev.core);

events XkbEvent
XkbEvent
events