Skip to content

libobs: Map X11 keysyms to the key that types them unshifted - #350

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/12941-x11-keysym-keycode
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/12941-x11-keysym-keycode

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

On X11, hotkeys bound to [, ] and similar symbols now use the key that types them. Before, a second keyboard layout group that also has the symbol on a number key turned the hotkey into that number.

  • Qt reports the pressed key as a keysym, and obs_nix_x11_key_from_virtual_key() turns it into an obs_key_t through the keycode that has it. It searched the core keyboard map keycode by keycode, each with all its groups and levels, and took the first keycode that had the keysym anywhere. With English + German, or the reporter's Latin + Cyrillic layout built on xkb latin, the 8 key (keycode 17) has [ at group 2, level 3 (AltGr+8). It comes before the [ key (keycode 34), so [ became OBS_KEY_8, ] became OBS_KEY_9 and { OBS_KEY_7.
  • As a result, Settings -> Hotkeys showed "8" for [, and outside OBS only the 8 key triggered the hotkey.
  • The fix searches the map one column (group and level) at a time, as Xlib's XKeysymToKeycode() does, so the key that types the symbol unshifted in the first group wins. A symbol that only exists on a shifted or AltGr level (German [ on AltGr+8 with no second group, * on Shift+8) still maps to the key that has it.
  1. test/cmocka: Add failing test for X11 hotkeys on "[" with a second layout: test_x11_hotkey_layout loads the reporter's core keyboard map rows for the 8, 9, [ and ] keys into the X server, starts libobs on X11 and restores the rows afterwards. A second connection stays open throughout, so the server keeps the map. The cases are:

    • [ and ] map to OBS_KEY_BRACKETLEFT and OBS_KEY_BRACKETRIGHT;
    • guard: symbols that only exist on a shifted or AltGr level (*, the square root and trademark signs, {) still map to the key that has them;
    • the label Settings -> Hotkeys shows for a [ hotkey (obs_key_to_str()) is [;
    • with OBS not focused, the [ key fires a [ hotkey and the 8 key does not (key presses through XTEST, picked up by the real hotkey thread).

    Today [ maps to OBS_KEY_8, the hotkey is shown as "8", and only the 8 key fires it ([ key 0 times, 8 key once). 3 of 4 fail.

  2. libobs: Map X11 keysyms to the key that types them unshifted: switches the search to one column at a time. [ and ] map to their own keys, the hotkey is shown as [ and fires from the [ key only, and the guard is unchanged. 4 of 4 pass.

Motivation and Context

Fixes #349 (upstream obsproject/obs-studio#12941, reported by Blayung, who also shared the layout that made the cause clear and checked that the Flatpak build behaves the same). Penwy triaged it upstream.

There is no upstream PR for this; libobs/obs-nix-x11.c has not changed upstream since 2024.

Why this one: anyone on X11 with two layout groups, a common setup for people who type in two languages, cannot bind [ or ] without them landing on a number key. The bound number then also triggers the action while typing in other programs.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_x11_hotkey_layout: 3 of 4 cases fail at the RED commit and 4 of 4 pass at GREEN, under a private Xvfb, with the installed system OpenGL module masked (the test does not load GL). ctest -V shows every case run, none skipped. Repeated 50 times at GREEN: same result every time. The test restores the keyboard map rows exactly.
  • Full ctest at GREEN: 112 of 112 in each lane (rebased on current master). Without a display, or on an X server without XTEST, the new test's cases skip.
  • The reporter's real layout: compiled with xkbcomp from their published symbols file and loaded into a private Xvfb, it gives the same core map rows the test uses. Against it, unfixed libobs maps [, ] and { to OBS_KEY_8, OBS_KEY_9 and OBS_KEY_7 with labels "8", "9" and "7"; the fix gives [, ] and [. The same holds for setxkbmap -layout us,de. With de alone, [ maps to the 8 key before and after, as it should.
  • clang-format, gersemi and ci/test_registration.py are clean. The test is registered on Linux only, when X11 and XTest are found.

Not tested:

  • The OBS UI. The test calls obs_key_from_virtual_key() with the keysym Qt passes for the key event, as OBSHotkeyEdit does, and obs_key_to_str() for the label.
  • Pressing AltGr+8 for [ while the second group is active now binds the [ key rather than the 8 key, the same as XKeysymToKeycode(). Telling these apart would need the keycode from Qt, not the keysym; that is a larger change to the frontend contract and is not part of this PR.
  • A real seat or a desktop session. Native Wayland hotkeys do not use this function; OBS running through XWayland does, and takes the same X11 path as the test.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 08:58
rdoupe and others added 2 commits October 12, 2026 02:03
…yout

A hotkey bound to "[" must show as "[" and fire from the [ key, also
when another layout group has "[" on a different key. The reporter's
layout has a Latin and a Cyrillic group; the Cyrillic group includes
xkb "latin", whose 8 key is [ 8, asterisk, bracketleft, trademark ]. The
X server's core keyboard map then lists bracketleft on the 8 key
(group 2, level 3) as well as unshifted on the [ key.

The test loads that layout's core map rows for the 8, 9, [ and ] keys
(the rows the X server reports after xkbcomp loads the reporter's
layout) through a connection that stays open, starts libobs on X11, and
restores the rows afterwards. Cases:
- "[" and "]" map to OBS_KEY_BRACKETLEFT and OBS_KEY_BRACKETRIGHT;
- symbols that only exist on a shifted or AltGr level ("*", the
  square root and trademark signs, "{") still map to the key that has
  them (guard);
- the label Settings -> Hotkeys shows for a "[" hotkey is "[";
- with OBS not focused, the [ key fires a "[" hotkey and the 8 key does
  not (key presses through XTEST, picked up by the hotkey thread).

On master, under xvfb, "[" maps to OBS_KEY_8, the hotkey is shown as
"8", and only the 8 key fires it; the guard passes. Without a display,
or without XTEST, every case skips.

Reported upstream in obsproject/obs-studio#12941 by Blayung.

Refs obs-rust#349.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
On X11, a hotkey bound to "[" was stored and shown as "8", and outside
OBS only the 8 key triggered it, when a second layout group has "[" on
the 8 key (for example English + German, or the reporter's Latin +
Cyrillic layout built on xkb "latin": AltGr+8 types "[").

Qt reports the pressed key as a keysym, and
obs_nix_x11_key_from_virtual_key() turns it into an obs_key_t through
the keycode that has it. It searched the core keyboard map keycode by
keycode, each with all its groups and levels, and took the first
keycode that had the keysym anywhere. Keycode 17 (the 8 key, with "["
at group 2, level 3) comes before keycode 34 (the [ key, with "["
unshifted in group 1), so "[" became OBS_KEY_8, "]" became OBS_KEY_9
and "{" OBS_KEY_7.

Search the map one column (group and level) at a time instead, as
XKeysymToKeycode() does, so the key that types the symbol unshifted in
the first group wins. A symbol that only exists on a shifted or AltGr
level (German "[" on AltGr+8 with no second group, "*" on Shift+8) still
maps to the key that has it.

With the fix, under xvfb (both ENABLE_RUST_LIBOBS lanes), "[" and "]"
map to OBS_KEY_BRACKETLEFT and OBS_KEY_BRACKETRIGHT, a "[" hotkey is
shown as "[" and fires from the [ key and not from the 8 key; the
shifted-level guard is unchanged. The same holds with the reporter's
layout and with "us,de" loaded through xkbcomp/setxkbmap; with "de"
alone "[" still maps to the 8 key.

Reported upstream in obsproject/obs-studio#12941 by Blayung.

Fixes obs-rust#349.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=6cb73637c43f3e989c99b1c5f6e24b0ecd5e951e secs=154 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791795989,"gate":"rust/all/fmt","host":"linux-x86_64","key":"1310046d0c92d070b5d756166d3f1a04b4c0c211f70e4cd3a2d301698fa5e7ea","lane":"lint","parents":["aa921c52ad466045348a52eaa8d9154c3c2a492c"],"secs":55,"stamp":"9e45679b0e1ed747efc7a66a11bbe2ad","tree":"6cb73637c43f3e989c99b1c5f6e24b0ecd5e951e","v":1,"via":"run"}
Ci-Attestation: {"at":1791795989,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"1310046d0c92d070b5d756166d3f1a04b4c0c211f70e4cd3a2d301698fa5e7ea","lane":"lint","parents":["aa921c52ad466045348a52eaa8d9154c3c2a492c"],"secs":55,"stamp":"aedf1724d7e8793401560b86d8c834cf","tree":"6cb73637c43f3e989c99b1c5f6e24b0ecd5e951e","v":1,"via":"run"}
Ci-Attestation: {"at":1791795989,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"cd66b81a7c115530af5730e677d9f8ff52529bc1c564ef17e8aa9b149dae221b","lane":"rust-test","parents":["aa921c52ad466045348a52eaa8d9154c3c2a492c"],"secs":48,"stamp":"f0ceed04556016160c91cdaf54e45ab3","tree":"6cb73637c43f3e989c99b1c5f6e24b0ecd5e951e","v":1,"via":"run"}
Ci-Attestation: {"at":1791795989,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"ae56690ed7930560897972e09e3bdc38f5e1b029b7aaf3ba5e2e1c9c2ada52ff","lane":"tier2-linux","parents":["aa921c52ad466045348a52eaa8d9154c3c2a492c"],"secs":51,"stamp":"3496d682803706727651686a0b0a9d3e","tree":"6cb73637c43f3e989c99b1c5f6e24b0ecd5e951e","v":1,"via":"run"}
@zackees
zackees force-pushed the fix/12941-x11-keysym-keycode branch from 90fed73 to f4c3962 Compare October 12, 2026 09:06
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified RED→GREEN locally (Tier 2 image + libxtst-dev, run under xvfb-run):

  • 2d9151e (test only): 3 failed / 1 passed — brackets_map_to_bracket_keys, bracket_hotkey_label, bracket_hotkey_fires_from_bracket_key
  • 90fed73 (fix): 4/4 passed

Note: the default Tier 2 image lacks libXtst, so CI builds skip this test. Rebased on master, local gate passed, and pushed as f4c3962.

@zackees
zackees merged commit 6487c74 into obs-rust:master Oct 12, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs: X11 hotkey on "[" shows and fires as "8" with a second keyboard layout (upstream #12941)

2 participants