Skip to content

Use the user's keyboard layout from the x11 system#210

Open
TristanCacqueray wants to merge 2 commits into
RustAudio:masterfrom
TristanCacqueray:user-xkb
Open

Use the user's keyboard layout from the x11 system#210
TristanCacqueray wants to merge 2 commits into
RustAudio:masterfrom
TristanCacqueray:user-xkb

Conversation

@TristanCacqueray

Copy link
Copy Markdown

This change uses xkbcommon to decode the key utf8 value based on the user's current layout. The implementation is adapted from the x11rb's xkbcommon-example.

@TristanCacqueray TristanCacqueray force-pushed the user-xkb branch 2 times, most recently from aeed9b9 to 8b421a4 Compare October 2, 2025 16:11
@prokopyl prokopyl linked an issue May 31, 2026 that may be closed by this pull request

@prokopyl prokopyl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! And thanks for this PR! 🙂

I have yet to delve into the details of the XKB implementation, but one issue I see is that the xkbcommon-rs crate links directly against the xkbcommon library, which would mean hosts would fail to load a plugin altogether.

It's for this reason we have moved to using x11-dl for instance. I think this should use
xkbcommon-dl instead (which is also what winit uses).

There's also been quite a few changes to the X11 codebase since you first submitted this PR, so if you have the time to rebase and update it, it would be very helpful!

@TristanCacqueray

Copy link
Copy Markdown
Author

Hey, alas I'm not sure how to get the keymap with xkbcommon-dl. Previously we could use get_core_keyboard_device_id, but now it looks like we need a name (e.g. for xkb_keymap_new)?

@prokopyl

Copy link
Copy Markdown
Member

Hey, alas I'm not sure how to get the keymap with xkbcommon-dl. Previously we could use get_core_keyboard_device_id, but now it looks like we need a name (e.g. for xkb_keymap_new)?

So it looks like xkbcommon-dl has a x11 feature that exposes xkb_x11_get_core_keyboard_device_id & other XCB functions that we need. It's just that they are not shown on docs.rs (probably because they haven't configured the x11 feature to show up there), but it is available there!

@TristanCacqueray TristanCacqueray force-pushed the user-xkb branch 2 times, most recently from dcccd6b to 871f2bd Compare June 13, 2026 02:37
@TristanCacqueray

TristanCacqueray commented Jun 13, 2026

Copy link
Copy Markdown
Author

@prokopyl thanks, that makes sense now. So I updated the PR and it seems to work as expected: in the open_window example, using a french layout, pressing the keycode for 2 emit the proper é Character. To be honest I'm not super familiar with xkbcommon, and even less with these -dl variants, so I'd be happy to rework this change if needed.

Do we need a safe fallback in case xkbcommon_option is None, e.g. to handle a missing libxkbcommon-x11.so ?

This change uses xkbcommon to decode the key utf8 value based
on the user's current layout. The implementation is adapted from
the x11rb's xkbcommon-example.
@TristanCacqueray

Copy link
Copy Markdown
Author

I also confirm this works downstream, e.g. through https://codeberg.org/TristanCacqueray/egui-baseview/src/branch/xkbfix

@prokopyl

Copy link
Copy Markdown
Member

@prokopyl thanks, that makes sense now. So I updated the PR and it seems to work as expected: in the open_window example, using a french layout, pressing the keycode for 2 emit the proper é Character. To be honest I'm not super familiar with xkbcommon, and even less with these -dl variants, so I'd be happy to rework this change if needed.

Do we need a safe fallback in case xkbcommon_option is None, e.g. to handle a missing libxkbcommon-x11.so ?

Sweet, thanks! I have tested it and it works pretty much as expected on my French keyboard. 🙂

I think a fallback is always good to have yes. xkbcommon is pretty much ubiquitous on destkop Linux OSes, but it's Linux so there's no guarantee.
To me the fallback could be as simple as just using the previous behavior (from before this PR). That way it would fall back to pretty much raw key-codes that only work for QWERTY, which is good enough in my opinion.

This change fallback to the old behavior when XkbcommonState can't
be created.
@TristanCacqueray

Copy link
Copy Markdown
Author

@prokopyl please let me know if this PR needs more change, I'm looking forward getting this integrated. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keyboard layout not accounted for in keyboard events [X11]

2 participants