# Replacing the Keyboard "On-the-Fly"

XKB supports the XkbNewKeyboardNotify event, which reports a change in keyboard geometry or the range of supported keycodes. The server can generate an XkbNewKeyboardNotify event when it detects a new keyboard, or in response to an XkbGetKeyboardByName request (see Using the Server’s Database of Keyboard Components) which loads a new keyboard description.

When a client opens a connection to the X server, the server reports the minimum and maximum keycodes. If the range of supported keycodes is changed, XKB keeps track of the minimum and maximum keycodes that were reported to each client and filters out any events that fall outside of that range. Note that these events are simply ignored; they are not delivered to some other client.

When the server sends an XkbNewKeyboardNotify event to a client to inform it of the new keycode range, XKB resets the stored range of legal keycodes to the keycode range reported in the event. Non-XKB clients and XKB-aware clients that do not request XkbNewKeyboardNotify events never receive events from keys that fall outside of the legal range that XKB maintains for that client.

When a client requests XkbNewKeyboardNotify events, the server compares the range of keycodes for the current keyboard to the range of keycodes that are valid for the client. If they are not the same, the server immediately sends that client an XkbNewKeyboardNotify event. Even if the "new" keyboard is not new to the server, it is new to this particular client.

In addition to filtering out-of-range key events, XKB:

- Adjusts core protocol MappingNotify events to refer only to keys that match the stored legal range.

- Reports keyboard mappings for keys that match the stored legal range to clients that issue a core protocol GetKeyboardMapping request.

- Reports modifier mappings only for keys that match the stored legal range to clients that issue a core protocol GetModifierMapping request.

- Restricts the core protocol ChangeKeyboardMapping and SetModifierMapping requests to keys that fall inside the stored legal range.

In short, XKB does everything possible to hide the fact that the range of legal keycodes has changed from clients non-XKB clients, which cannot be expected to deal with it. The corresponding XKB events and requests do not pay attention to the legal keycode range in the same way because XKB makes it possible for clients to track changes to the keycode range for a device and respond to them.