Keyboard state encompasses all of the transitory information necessary to map a physical key press or release to an appropriate event. The Xkb keyboard state consists of primitive components and additional derived components that are maintained for efficiency reasons. Figure 5.1 shows the components of Xkb keyboard state and their relationships.
</para>
<mediaobject>
<!-- <title>Keyboard State Description</title> -->
The Xkb keyboard state is comprised of the state of all keyboard modifiers, the keyboard group, and the state of the pointer buttons. These are grouped into the following components:
</para>
<itemizedlist>
<listitem>
<para>
The locked group and locked modifiers
</para>
</listitem>
<listitem>
<para>
The latched group and latched modifiers
</para>
</listitem>
<listitem>
<para>
The base group and base modifiers
</para>
</listitem>
<listitem>
<para>
The effective group and effective modifiers
</para>
</listitem>
<listitem>
<para>
The state of the core pointer buttons
</para>
</listitem>
</itemizedlist>
<para>
The <emphasis>
modifiers</emphasis>
are <emphasis>
Shift</emphasis>
, <emphasis>
Lock</emphasis>
, <emphasis>
Control</emphasis>
, and <emphasis>
Mod1</emphasis>
-<emphasis>
Mod5</emphasis>
, as defined by the core protocol. A modifier can be thought of as a toggle that is either set or unset. All modifiers are initially unset. When a modifier is locked, it is set and remains set for all future key events, until it is explicitly unset. A latched modifier is set, but automatically unsets after the next key event that does not change the keyboard state. Locked and latched modifier state can be changed by keyboard activity or via Xkb extension library functions.
</para>
<para>
The Xkb extension provides support for <emphasis>
keysym</emphasis>
<emphasis>
groups</emphasis>
, as defined by ISO9995:
</para>
<variablelist>
<varlistentry>
<term>Group</term>
<listitem>
<para>
A logical state of a keyboard providing access to a collection of characters.
A group usually contains a set of characters that logically belong together
and that may be arranged on several shift levels within that group.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
The Xkb extension supports up to four keysym groups. Groups are named beginning with one and indexed beginning with zero. All group states are indicated using the group index. At any point in time, there is zero or one locked group, zero or one latched group, and one base group. When a group is locked, it supersedes any previous locked group and remains the locked group for all future key events, until a new group is locked. A latched group applies only to the next key event that does not change the keyboard state. The locked and latched group can be changed by keyboard activity or via Xkb extension library functions.
</para>
<para>
Changing to a different group changes the keyboard state to produce characters from a different group. Groups are typically used to switch between keysyms of different languages and locales.
</para>
<para>
The <emphasis>
pointer buttons</emphasis>
are <emphasis>
Button1</emphasis>
- <emphasis>
Button5</emphasis>
, as defined by the core protocol.
</para>
<para>
The <emphasis>
base group</emphasis>
and <emphasis>
base modifiers</emphasis>
represent keys that are physically or logically down. These
and the pointer buttons can be changed by keyboard activity and
not by Xkb requests. It is possible for a key to be logically
down, but not physically down, and neither latched nor locked.
<footnote><para>
Keys may be logically down when they are physically up because
of their electrical properties or because of the keyboard extension
in the X server having filtered the key release, for esoteric reasons.
</para></footnote>
</para>
<para>
The <emphasis>
effective modifiers</emphasis>
are the bitwise union of the locked, latched, and the base modifiers.
</para>
<para>
The <emphasis>
effective group</emphasis>
is the arithmetic sum of the group indices of the latched group, locked group, and base group, which is then normalized by some function. The result is a meaningful group index.
</para>
<simplelisttype='vert'columns='1'>
<member>
n = number of keyboard groups, 1<= n <= 4
</member>
<member>
0 <= any of locked, latched, or base group < n
</member>
<member>
effective group = f(locked group + latched group + base group)
</member>
</simplelist>
<para>
The function f ensures that the effective group is within range. The precise function is specified for the keyboard and can be retrieved through the keyboard description. It may wrap around, clamp down, or default. Few applications will actually examine the effective group, and far fewer still will examine the locked, latched, and base groups.
</para>
<para>
There are two circumstances under which groups are normalized:
</para>
<orderedlist>
<listitem><para>
The global locked or effective group changes. In this case, the changed group is normalized into range according to the settings of the <emphasis>
groups_wrap</emphasis>
field of the <emphasis>
XkbControlsRec</emphasis>
structure for the keyboard (see section 10.7.1). <!-- xref -->
</para></listitem>
<listitem><para>
The Xkb library is interpreting an event with an effective group that is legal for the keyboard as a whole, but not for the key in question. In this case, the group to use for this event only is determined using the <emphasis>
group_info</emphasis>
field of the key symbol mapping (<emphasis>
XkbSymMapRec</emphasis>
) for the event key.
</para></listitem>
</orderedlist>
<para>
Each nonmodifier key on a keyboard has zero or more symbols, or keysyms, associated with it. These are the logical symbols that the key can generate when it is pressed. The set of all possible keysyms for a keyboard is divided into groups. Each key is associated with zero or more groups; each group contains one or more symbols. When a key is pressed, the determination of which symbol for the key is selected is based on the effective group and the shift level, which is determined by which modifiers are set.
</para>
<para>
A client that does not explicitly call Xkb functions, but that otherwise makes use of an X library containing the Xkb extension, will have keyboard state represented in bits 0 - 14 of the state field of events that report modifier and button state. Such a client is said to be <emphasis>
Xkb-capable</emphasis>
. A client that does explicitly call Xkb functions is an <emphasis>
Xkb-aware</emphasis>
client. The Xkb keyboard state includes information derived from the effective state and from two server parameters that can be set through the keyboard extension. The following components of keyboard state pertain to Xkb-capable and Xkb-aware clients:
</para>
<itemizedlist>
<listitem>
<para>
lookup state: lookup group and lookup modifiers
</para>
</listitem>
<listitem>
<para>
grab state: grab group and grab modifiers
</para>
</listitem>
</itemizedlist>
<para>
The <emphasis>
lookup modifiers</emphasis>
and <emphasis>
lookup group</emphasis>
are represented in the state field of core X events. The modifier state and keycode of a key event are used to determine the symbols associated with the event. For <emphasis>
KeyPress</emphasis>
and <emphasis>
KeyRelease</emphasis>
events, the lookup modifiers are computed as:
</para>
<literallayout>
((base | latched | locked) & ~<emphasis> server_internal_modifiers</emphasis>)
</literallayout>
<para>
Otherwise the lookup modifiers are computed as:
</para>
<literallayout>
(((base | latched | (locked & ~<emphasis> ignore_locks</emphasis>)) & ~<emphasis> server_internal_modifiers</emphasis>)
</literallayout>
<para>
The lookup group is the same as the effective group.
</para>
<para>
When an Xkb-capable or Xkb-aware client wishes to map a keycode to a keysym, it should use the <emphasis>
lookup state</emphasis>
— the lookup group and the lookup modifiers.
</para>
<para>
The <emphasis>
grab state</emphasis>
is the state used when matching events to passive grabs. If the event activates a grab, the <emphasis>
grab modifiers</emphasis>
and <emphasis>
grab group</emphasis>
are represented in the state field of core X events; otherwise, the lookup state is used. The grab modifiers are computed as:
</para>
<literallayout>
(((base | latched | (locked & ~ignore_locks)) & ~server_internal_modifiers)
</literallayout>
<para>
If the server’s <emphasis>
IgnoreGroupLock</emphasis>
control (see section 10.7.3) is not set, the grab group is the same as the effective group. Otherwise, the grab group is computed from the base group and latched group, ignoring the locked group.
</para>
<para>
The final three components of Xkb state are applicable to clients that are not linked with an Xlib containing the X keyboard extension library and therefore are not aware of the keyboard extension (<emphasis>
Xkb-unaware </emphasis>
clients):
</para>
<itemizedlist>
<listitem>
<para>
The compatibility modifier state
</para>
</listitem>
<listitem>
<para>
The compatibility lookup modifier state
</para>
</listitem>
<listitem>
<para>
The compatibility grab modifier state
</para>
</listitem>
</itemizedlist>
<para>
The X11 protocol interpretation of modifiers does not include direct support for multiple groups. When an Xkb-extended X server connects to an Xkb-unaware client, the compatibility states remap the keyboard group into a core modifier whenever possible. The compatibility state corresponds to the effective modifier and effective group state, with the group remapped to a modifier. The compatibility lookup and grab states correspond to the lookup and grab states, respectively, with the group remapped to a modifier. The compatibility lookup state is reported in events that do not trigger passive grabs; otherwise, the compatibility grab state is reported.
function queries the server for the current keyboard state, waits for a reply, and then backfills <emphasis>
state_return</emphasis>
with the results.
</para>
<para>
All group values are expressed as group indices in the range [0..3]. Modifiers and the compatibility modifier state values are expressed as the bitwise union of the core X11 modifier masks. The pointer button state is reported as in the core X11 protocol.
events to clients wanting notification whenever the Xkb state changes. The changes reported include changes to any aspect of the keyboard state: when a modifier is set or unset, when the current group changes, or when a pointer button is pressed or released. As with all Xkb events, <emphasis>
XkbStateNotify</emphasis>
events are reported to all interested clients without regard to the current keyboard input focus or grab state.
</para>
<para>
There are many different types of Xkb state changes. Xkb defines an event detail mask corresponding to each type of change. The event detail masks are listed in Table 5.3.