Advertisement

Linux/X11 - Device independent physical key locations

Started by June 13, 2022 08:06 AM
7 comments, last by Tom Sloper 1 year, 8 months ago

Hi,

I´m currently implementing keyboard input for my game engine. For communication with the X Server (Ubuntu,Linux) I use xlib with the XKB extension. As far as I understand it the keyboard sends a hardware dependent scancode which gets somehow transformed into a device independent keycode (i guess via evdev ?). Every keycode can also have multiple keysyms associated with it which encode the character on the key cap depending on the modifier keys (shift, alt, numlock …).

Most of the time some keys should not depend on the keyboard layout like the famous W A S D movement. Thats why we need a way to describe the “physical location” of a key rather than the “logical value” (if this makes sense). In some sources it is stated that the X11 keycodes are device independent but after some testing with my razor huntsman and my keychron k6 (65% keyboard) I can say that some keys produce different keycodes altough they keys have the same physical key location. For example the windows key (last row, second key from left) on my huntsman produces keycode 133 while the key in the same location on my keychron produces keycode 64. Other keys like A-Z, 0-9, PageUp, PageDown produces the same keycodes.

Now my problem is that I dont know how I can describe the physical key location in a device independent manner with xlib on linux across keyboards with different physical layouts. Can you give me some tips in how to “solve” this problem ? Maybe I´m also confused and got something wrong D:

Thank you !

Not an X11 expert, but I think you can define the model of your keyboard in the X11 configuration.

I am however not sure why you need physical location of the keys. Except for text-entry, different users want different keys to steer their vehicle or fire their weapon, so you want to make the key controls configurable anyway. So instead ask the user to press a key for some action and at that time record its key-code, and done?

A second thing is, why X11? Libraries like SFML or SDL2 provide hardware and OS inter-action so you don't need to do that. They are also cross-platform, so your engine will work at other platforms like Windows.

Advertisement

Alberth said:
I am however not sure why you need physical location of the keys. Except for text-entry, different users want different keys to steer their vehicle or fire their weapon, so you want to make the key controls configurable anyway. So instead ask the user to press a key for some action and at that time record its key-code, and done?

You are right it is probably not needed and different users want different key bindings but I can't imagine that I'm the first person who wants to implement this. W/A/S/D for walking is default in many games and I guess this should work out of the box with every layout. I´m pretty sure that it works with most keyboards because when I change the logical layout and/or the language I always get the same keycodes. It only starts when I use other “form factor” keyboards and presumably keyboards with other physical layouts. But maybe this it how it works and completely fine ? But then there would be the question how I could account for that in a sane way.

Alberth said:
A second thing is, why X11? Libraries like SFML or SDL2 provide hardware and OS inter-action so you don't need to do that.

I want to do that on my own. This engine will never be used by anyone it is just a small toy project ^^

Alberth said:
Not an X11 expert, but I think you can define the model of your keyboard in the X11 configuration.

You are right. With XKB this should be possible and would probably solve the problem (at least for me).

Thanks for your reply !

**EDIT

I dont know why my brain is this way but my keychron keyboard has two modes: windows/linux and macos. While testing the switch was always in macos position. If I change it to windows/linux I get the same results as with my huntsman keyboard. Sorry

I've had such a feature recommended to me, as I recall. In short, it means that a given control-layout should work out-of-the-box across various keyboards, with no remapping required, regardless of what actual characters are associated with them.

After all, for the purposes of many games, it doesn't really matter that it's, say, “W” that one presses to move forward--it matters that it's the key located at the top of a rough triangle near the left-hand side of the keyboard.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

@Thaumaturge Did you implement it and if so, how did you do that ? That is exactly what I wanted to know ^^

@ThatJenkins01 Ah, sorry--I use a game-engine that provides such functionality “out-of-the-box”. Thus for me it was more a matter of making use of the relevant feature than of actually implementing overmuch.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Advertisement

I can help you with your keyboard input implementation. You're correct that X11 keycodes are device independent, but they may not be layout independent.

Please don't necro. Thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement