Skip to content

Add conversions from/to XKB codes#98

Open
chrisduerr wants to merge 2 commits into
rust-windowing:mainfrom
chrisduerr:xkb_conversion
Open

Add conversions from/to XKB codes#98
chrisduerr wants to merge 2 commits into
rust-windowing:mainfrom
chrisduerr:xkb_conversion

Conversation

@chrisduerr
Copy link
Copy Markdown

The keyboard-types crate is useful for having a standardized target for various shared keyboard functionality, however most applications will receive keys in the system's format.

This patch adds a translation layer from system XKB key codes to keyboard-types, allowing applications to easily convert between the two without having to rely on copy/pasting the same conversion logic between several projects.

Currently the conversion to XKB codes is only available for keycodes, not keysyms, since the latter does not have a unique conversion.

See #97.


Only doing XKB here since I don't have access to the other platforms, however I believe that this on its own is already of value and it does not significantly harm any of the other platforms (especially once LTO tosses all this code out during compile).

I've gone with an approach that pulls zero additional dependencies, allowing these functions to always be available without feature flags.

Comment thread src/key.rs
/// Get the [`NamedKey`] for a an XKB keysym.
pub fn from_xkb_keysym(keysym: u32) -> Option<Key> {
match keysym {
0x20 => Some(Key::Character(" ".into())),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Winit uses the xkbcommon_dl crate here for predefined keysym aliases. Technically it would have been easier to do the same, but I did not want to rely on any dependencies.

This does however make this code a little less maintainable, since you'll essentially have to trust that I picked all the correct values (or that my machine-conversion with vim macros did the right thing). Considering the same applies to keycodes, I think this should be fine, but I can see it both ways.

The `keyboard-types` crate is useful for having a standardized target
for various shared keyboard functionality, however most applications
will receive keys in the system's format.

This patch adds a translation layer from system XKB key codes to
`keyboard-types`, allowing applications to easily convert between the
two without having to rely on copy/pasting the same conversion logic
between several projects.

Currently the conversion to XKB codes is only available for keycodes,
not keysyms, since the latter does not have a unique conversion.

See rust-windowing#97.
@chrisduerr
Copy link
Copy Markdown
Author

I should probably mention that the keysym -> Key conversion does not fully handle the conversion, since text characters are not converted. This could be changed, but would require an external dependency I believe (one which would go against a system lib, so even 'worse' than normal).

@chrisduerr
Copy link
Copy Markdown
Author

Any feedback would be appreciated, been a while now.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant