1 <chapter id='Keyboard_State'>
2 <title>Keyboard State</title>
5 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.
9 <!-- <title>Keyboard State Description</title> -->
10 <imageobject> <imagedata format="SVG" fileref="XKBlib-2.svg"/>
12 <caption>Xkb State</caption>
16 <sect1 id='Keyboard_State_Description'>
17 <title>Keyboard State Description</title>
19 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:
25 The locked group and locked modifiers
30 The latched group and latched modifiers
35 The base group and base modifiers
40 The effective group and effective modifiers
45 The state of the core pointer buttons
63 , 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.
68 The Xkb extension provides support for <emphasis>
72 , as defined by ISO9995:
81 A logical state of a keyboard providing access to a collection of characters.
82 A group usually contains a set of characters that logically belong together
83 and that may be arranged on several shift levels within that group.
92 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.
97 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.
103 pointer buttons</emphasis>
108 , as defined by the core protocol.
114 base group</emphasis>
116 base modifiers</emphasis>
117 represent keys that are physically or logically down. These
118 and the pointer buttons can be changed by keyboard activity and
119 not by Xkb requests. It is possible for a key to be logically
120 down, but not physically down, and neither latched nor locked.
122 Keys may be logically down when they are physically up because
123 of their electrical properties or because of the keyboard extension
124 in the X server having filtered the key release, for esoteric reasons.
132 effective modifiers</emphasis>
133 are the bitwise union of the locked, latched, and the base modifiers.
139 effective group</emphasis>
140 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.
143 <simplelist type='vert' columns='1'>
145 n = number of keyboard groups, 1<= n <= 4
149 0 <= any of locked, latched, or base group < n
153 effective group = f(locked group + latched group + base group)
158 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.
163 There are two circumstances under which groups are normalized:
168 The global locked or effective group changes. In this case, the changed group is normalized into range according to the settings of the <emphasis>
169 groups_wrap</emphasis>
170 field of the <emphasis>
171 XkbControlsRec</emphasis>
172 structure for the keyboard (see section 10.7.1). <!-- xref -->
176 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>
177 group_info</emphasis>
178 field of the key symbol mapping (<emphasis>
179 XkbSymMapRec</emphasis>
185 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.
190 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>
191 Xkb-capable</emphasis>
192 . A client that does explicitly call Xkb functions is an <emphasis>
194 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:
200 lookup state: lookup group and lookup modifiers
205 grab state: grab group and grab modifiers
212 lookup modifiers</emphasis>
214 lookup group</emphasis>
215 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>
218 KeyRelease</emphasis>
219 events, the lookup modifiers are computed as:
223 ((base | latched | locked) & ~<emphasis> server_internal_modifiers</emphasis>)
227 Otherwise the lookup modifiers are computed as:
231 (((base | latched | (locked & ~<emphasis> ignore_locks</emphasis>)) & ~<emphasis> server_internal_modifiers</emphasis>)
235 The lookup group is the same as the effective group.
240 When an Xkb-capable or Xkb-aware client wishes to map a keycode to a keysym, it should use the <emphasis>
241 lookup state</emphasis>
242 — the lookup group and the lookup modifiers.
248 grab state</emphasis>
249 is the state used when matching events to passive grabs. If the event activates a grab, the <emphasis>
250 grab modifiers</emphasis>
252 grab group</emphasis>
253 are represented in the state field of core X events; otherwise, the lookup state is used. The grab modifiers are computed as:
257 (((base | latched | (locked & ~ignore_locks)) & ~server_internal_modifiers)
261 If the server’s <emphasis>
262 IgnoreGroupLock</emphasis>
263 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.
268 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>
269 Xkb-unaware </emphasis>
276 The compatibility modifier state
281 The compatibility lookup modifier state
286 The compatibility grab modifier state
292 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.
297 <sect1 id='Changing_the_Keyboard_State'>
298 <title>Changing the Keyboard State</title>
300 <sect2 id='Changing_Modifiers'>
301 <title>Changing Modifiers</title>
304 The functions in this section that change the use of modifiers use a mask in the parameter <emphasis>
306 . It is a bitwise inclusive OR of the legal modifier masks:
310 <title>Real Modifier Masks</title>
311 <?dbfo keep-together="always" ?>
312 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
313 <colspec colname='c1' colwidth='1.0*'/>
319 <entry>ShiftMask</entry>
322 <entry>LockMask</entry>
325 <entry>ControlMask</entry>
328 <entry>Mod1Mask</entry>
331 <entry>Mod2Mask</entry>
334 <entry>Mod3Mask</entry>
337 <entry>Mod4Mask</entry>
340 <entry>Mod5Mask</entry>
347 To lock and unlock any of the eight real keyboard modifiers, use <emphasis>
348 XkbLockModifiers:</emphasis>
351 <informaltable frame='none'>
352 <?dbfo keep-together="always" ?>
353 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
354 <colspec colname='c1' colwidth='1.0*'/>
357 <entry role='functiondecl'>
358 Bool <emphasis> XkbLockModifiers</emphasis>
360 display, device_spec, affect, values</emphasis>
365 <entry role='functionargdecl'>
368 ; /* connection to the X server */
372 <entry role='functionargdecl'>
373 unsigned int <emphasis>
374 device_spec</emphasis>
375 ; /* device ID, or <emphasis>
376 XkbUseCoreKbd</emphasis>
381 <entry role='functionargdecl'>
382 unsigned int<emphasis>
384 ; /* mask of real modifiers whose lock state is to change */
388 <entry role='functionargdecl'>
389 unsigned int <emphasis>
391 ; /* 1 => lock, 0 => unlock; only for modifiers selected by <emphasis>
402 XkbLockModifiers</emphasis>
403 sends a request to the server to lock the real modifiers selected by both <emphasis>
407 and to unlock the real modifiers selected by <emphasis>
409 but not selected by <emphasis>
412 XkbLockModifiers</emphasis>
413 does not wait for a reply from the server. It returns <emphasis>
415 if the request was sent, and <emphasis>
422 To latch and unlatch any of the eight real keyboard modifiers, use <emphasis>
423 XkbLatchModifiers:</emphasis>
427 <informaltable frame='none'>
428 <?dbfo keep-together="always" ?>
429 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
430 <colspec colname='c1' colwidth='1.0*'/>
433 <entry role='functiondecl'>
435 XkbLatchModifiers</emphasis>
437 isplay, device_spec, affect, values</emphasis>
442 <entry role='functionargdecl'>
445 ; /* connection to the X server */
449 <entry role='functionargdecl'>
450 unsigned int <emphasis>
451 device_spec</emphasis>
452 ; /* device ID, or <emphasis>
453 XkbUseCoreKbd</emphasis>
458 <entry role='functionargdecl'>
459 unsigned int<emphasis>
461 ; /* mask of modifiers whose latch state is to change */
465 <entry role='functionargdecl'>
466 unsigned int <emphasis>values</emphasis>;
467 /* 1 => latch, 0 => unlatch; only for mods selected by <emphasis>
478 XkbLatchModifiers</emphasis>
479 sends a request to the server to latch the real modifiers selected by both <emphasis>
483 and to unlatch the real modifiers selected by <emphasis>
485 but not selected by <emphasis>
488 XkbLatchModifiers</emphasis>
489 does not wait for a reply from the server. It returns <emphasis>
491 if the request was sent, and <emphasis>
498 <sect2 id='Changing_Groups'>
499 <title>Changing Groups</title>
502 Reference the keysym group indices with these symbolic constants:
505 <table frame='topbot'>
506 <title>Symbolic Group Names</title>
507 <?dbfo keep-together="always" ?>
508 <tgroup cols='2' align='left' colsep='0' rowsep='0'>
509 <colspec colname='c1' colwidth='1.0*'/>
510 <colspec colname='c2' colwidth='2.0*'/>
513 <entry>Symbolic Name</entry>
517 <entry>XkbGroup1Index</entry>
521 <entry>XkbGroup2Index</entry>
525 <entry>XkbGroup3Index</entry>
529 <entry>XkbGroup4Index</entry>
537 To lock the keysym group, use <emphasis>
538 XkbLockGroup. </emphasis>
541 <informaltable frame='none'>
542 <?dbfo keep-together="always" ?>
543 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
544 <colspec colname='c1' colwidth='1.0*'/>
547 <entry role='functiondecl'>
549 XkbLockGroup</emphasis>
551 display, device_spec, group</emphasis>
556 <entry role='functionargdecl'>
559 ; /* connection to the X server */
563 <entry role='functionargdecl'>
564 unsigned int <emphasis>
565 device_spec</emphasis>
566 ; /* device ID, or <emphasis>
567 XkbUseCoreKbd</emphasis>
572 <entry role='functionargdecl'>
573 unsigned int <emphasis>
575 ; /* index of the keysym group to lock */
584 XkbLockGroup</emphasis>
585 sends a request to the server to lock the specified <emphasis>
587 and does not wait for a reply. It returns <emphasis>
589 if the request was sent and <emphasis>
596 To latch the keysym group, use <emphasis>
597 XkbLatchGroup.</emphasis>
601 <informaltable frame='none'>
602 <?dbfo keep-together="always" ?>
603 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
604 <colspec colname='c1' colwidth='1.0*'/>
607 <entry role='functiondecl'>
609 XkbLatchGroup</emphasis>
611 display, device_spec, group</emphasis>
616 <entry role='functionargdecl'>
619 ; /* connection to the X server */
623 <entry role='functionargdecl'>
624 unsigned int<emphasis>
625 device_spec</emphasis>
626 ; /* device ID, or <emphasis>
627 XkbUseCoreKbd</emphasis>
632 <entry role='functionargdecl'>
633 unsigned int<emphasis>
635 ; /* index of the keysym group to latch */
644 XkbLatchGroup</emphasis>
645 sends a request to the server to latch the specified group and does not wait for a reply. It returns <emphasis>
647 if the request was sent and <emphasis>
655 <sect1 id='Determining_Keyboard_State'>
656 <title>Determining Keyboard State</title>
659 Xkb keyboard state may be represented in an <emphasis>
660 XkbStateRec</emphasis>
664 <para><programlisting>
666 unsigned char group; /* effective group index */
667 unsigned char base_group; /* base group index */
668 unsigned char latched_group; /* latched group index */
669 unsigned char locked_group; /* locked group index */
670 unsigned char mods; /* effective modifiers */
671 unsigned char base_mods; /* base modifiers */
672 unsigned char latched_mods; /* latched modifiers */
673 unsigned char locked_mods; /* locked modifiers */
674 unsigned char compat_state; /* effective group => modifiers */
675 unsigned char grab_mods; /* modifiers used for grabs */
676 unsigned char compat_grab_mods; /* mods used for compatibility mode grabs */
677 unsigned char lookup_mods; /* modifiers used to lookup symbols */
678 unsigned char compat_lookup_mods; /* mods used for compatibility lookup */
679 unsigned short ptr_buttons; /* 1 bit => corresponding pointer btn is down */
681 XkbStateRec</emphasis>
683 </programlisting></para>
686 To obtain the keyboard state, use <emphasis>
687 XkbGetState.</emphasis>
690 <informaltable frame='none'>
691 <?dbfo keep-together="always" ?>
692 <tgroup cols='1' align='left' colsep='0' rowsep='0'>
693 <colspec colname='c1' colwidth='1.0*'/>
696 <entry role='functiondecl'>
698 XkbGetState</emphasis>
702 device_spec</emphasis>
704 state_return</emphasis>
709 <entry role='functionargdecl'>
712 ; /* connection to the X server */
716 <entry role='functionargdecl'>
717 unsigned int <emphasis>
718 device_spec</emphasis>
719 ; /* device ID, or <emphasis>
720 XkbUseCoreKbd</emphasis>
725 <entry role='functionargdecl'>
726 XkbStatePtr <emphasis>
727 state_return</emphasis>
728 ; /* backfilled with Xkb state */
737 XkbGetState </emphasis>
738 function queries the server for the current keyboard state, waits for a reply, and then backfills <emphasis>
739 state_return</emphasis>
745 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.
750 <sect1 id='Tracking_Keyboard_State'>
751 <title>Tracking Keyboard State</title>
754 The Xkb extension reports <emphasis>
755 XkbStateNotify </emphasis>
756 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>
757 XkbStateNotify</emphasis>
758 events are reported to all interested clients without regard to the current keyboard input focus or grab state.
763 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.
766 <table frame='topbot'>
767 <title>XkbStateNotify Event Detail Masks</title>
768 <?dbfo keep-together="always" ?>
769 <tgroup cols='2' align='left' colsep='0' rowsep='0'>
770 <colspec colname='c1' colwidth='1.0*'/>
771 <colspec colname='c2' colwidth='1.0*'/>
780 <entry>XkbModifierStateMask</entry>
781 <entry>(1L << 0)</entry>
784 <entry>XkbModifierBaseMask</entry>
785 <entry>(1L << 1)</entry>
788 <entry>XkbModifierLatchMask</entry>
789 <entry>(1L << 2)</entry>
792 <entry>XkbModifierLockMask</entry>
793 <entry>(1L << 3)</entry>
796 <entry>XkbGroupStateMask</entry>
797 <entry>(1L << 4)</entry>
800 <entry>XkbGroupBaseMask</entry>
801 <entry>(1L << 5)</entry>
804 <entry>XkbGroupLatchMask</entry>
805 <entry>(1L << 6)</entry>
808 <entry>XkbGroupLockMask</entry>
809 <entry>(1L << 7)</entry>
812 <entry>XkbCompatStateMask</entry>
813 <entry>(1L << 8)</entry>
816 <entry>XkbGrabModsMask</entry>
817 <entry>(1L << 9)</entry>
820 <entry>XkbCompatGrabModsMask</entry>
821 <entry>(1L << 10)</entry>
824 <entry>XkbLookupModsMask</entry>
825 <entry>(1L << 11)</entry>
828 <entry>XkbCompatLookupModsMask</entry>
829 <entry>(1L << 12)</entry>
832 <entry>XkbPointerButtonMask</entry>
833 <entry>(1L << 13)</entry>
836 <entry>XkbAllStateComponentsMask</entry>
837 <entry>(0x3fff)</entry>
844 To track changes in the keyboard state for a particular device, select to receive <emphasis>
845 XkbStateNotify</emphasis>
846 events by calling either <emphasis>
847 XkbSelectEvents</emphasis>
849 XkbSelectEventDetails</emphasis>
850 (see section 4.3). <!-- xref -->
855 To receive <emphasis>
856 XkbStateNotify</emphasis>
857 events under all possible conditions, use <emphasis>
858 XkbSelectEvents</emphasis>
860 XkbStateNotifyMask</emphasis>
862 bits_to_change</emphasis>
864 values_for_bits</emphasis>
870 To receive <emphasis>
871 XkbStateNotify</emphasis>
872 events only under certain conditions, use <emphasis>
873 XkbSelectEventDetails</emphasis>
875 XkbStateNotify</emphasis>
877 event_type</emphasis>
878 and specifying the desired state changes in <emphasis>
879 bits_to_change</emphasis>
881 values_for_bits</emphasis>
882 using mask bits from Table 5.3. <!-- xref -->
887 The structure for <emphasis>
888 XkbStateNotify</emphasis>
892 <para><programlisting>
894 int type; /* Xkb extension base event code */
895 unsigned long serial; /* X server serial number for event */
896 Bool send_event; /* <emphasis> True</emphasis> => synthetically generated */
897 Display * display; /* server connection where event generated */
898 Time time; /* server time when event generated */
899 int xkb_type; /* <emphasis> XkbStateNotify</emphasis> */
900 int device; /* Xkb device ID, will not be <emphasis> XkbUseCoreKbd</emphasis> */
901 unsigned int changed; /* bits indicating what has changed */
902 int group; /* group index of effective group */
903 int base_group; /* group index of base group */
904 int latched_group; /* group index of latched group */
905 int locked_group; /* group index of locked group */
906 unsigned int mods; /* effective modifiers */
907 unsigned int base_mods; /* base modifiers */
908 unsigned int latched_mods; /* latched modifiers */
909 unsigned int locked_mods; /* locked modifiers */
910 int compat_state; /* computed compatibility state */
911 unsigned char grab_mods; /* modifiers used for grabs */
912 unsigned char compat_grab_mods; /* modifiers used for compatibility grabs */
913 unsigned char lookup_mods; /* modifiers used to lookup symbols */
914 unsigned char compat_lookup_mods; /* mods used for compatibility look up */
915 int ptr_buttons; /* core pointer buttons */
916 KeyCode keycode; /* keycode causing event, 0 if programmatic */
917 char event_type; /* core event if <emphasis> req_major</emphasis> or
918 <emphasis> req_minor</emphasis> non zero */
919 char req_major; /* major request code if program trigger, else 0 */
920 char req_minor; /* minor request code if program trigger, else 0 */
921 } <emphasis>XkbStateNotifyEvent</emphasis>
923 </programlisting></para>
926 When you receive an <emphasis>
927 XkbStateNotify</emphasis>
928 event, the <emphasis>
930 field indicates which elements of keyboard state have changed.
931 This will be the bitwise inclusive OR of one or more of the <emphasis>
932 XkbStateNotify</emphasis>
933 event detail masks shown in Table 5.3. All fields reported in <!-- xref -->
934 the event are valid, but only those indicated in <emphasis>
943 field is the group index of the effective keysym group. The <emphasis>
944 base_group</emphasis>
946 latched_group</emphasis>
948 locked_group</emphasis>
949 fields are set to a group index value representing the base group,
950 the latched group, and the locked group, respectively. The X
951 server can set the modifier and compatibility state fields to
952 a union of the core modifier mask bits; this union represents the
953 corresponding modifier states. The <emphasis>ptr_button</emphasis>
954 field gives the state of the core pointer buttons as a
955 mask composed of an inclusive OR of zero or more of the
956 core pointer button masks.
961 Xkb state changes can occur either in response to keyboard
962 activity or under application control. If a key event
963 caused the state change, the <emphasis>
965 field gives the keycode of the key event, and the <emphasis>
966 event_type</emphasis>
967 field is set to either <emphasis>KeyPress</emphasis>
969 KeyRelease</emphasis>
970 . If a pointer button event caused the state change, the <emphasis>
972 field is zero, and the <emphasis>event_type</emphasis>
973 field is set to either <emphasis>ButtonPress</emphasis>
974 or <emphasis>ButtonRelease</emphasis>
975 . Otherwise, the major and minor codes of the request that caused the
976 state change are given in the <emphasis>
980 fields, and the <emphasis>
982 field is zero. The <emphasis>
984 value is the same as the major extension opcode.