Search code examples
linuxwindowsmacoskeyboard-layout

library with keyboard layouts


For my daughter (11 months old) I'm currently developing a program that entertains her while she is typing (well, more of smashing it) on the keyboard of my laptop. It works really nice already and as she and her nephew (same age) seem to like it, I thought that maybe other children might as well.

The problem I now got is the following: when a key is pressed, something is drawn on the screen. The location where the picture is drawn depends on the physical location of the key on the keyboard. So for that I need to know the layout of the connected keyboard. Currently I'm using hardcoded en_US layout but maybe some day an e.g. French kid wants to use it.

On linux I can find the locale using the LANG environment variable and I think I may be able to retrieve the layout from some undocumented file underneath the xorg/x11 files. But I'm looking for a more portable version which works between distributions but also on microsoft windows and maybe even macos x.

I googled for it of course but could not find anything. So does anyone know of a solution in C/C++? If it is full of #ifdef-statements to work on multiple platforms then that is not a problem.

the program this is all about


Solution

  • Since you are using SDL already, it would make sense to use the build-in keyboard input handling functionality. This should give you all information you could possibly get via software (from the scancode that the keyboard hardware delivers up to the semantic meaning of the pressed key based on the current keyboard layout).

    As keyboard manufacturer are free to build any physical layout of keys (some keyboards do not have any keys at all), a rocksolid solution to get the physical position of a pressed key on all keyboards is close to impossible. And since usually only the meaning of the pressed key is of interest there little to no use cases for which such a library should be developed. Thus, I doubt there are any libraries to help you with that particular problem.

    As some commenters mentioned, I think you get a good solution for most keyboards by using the scancode value that you are able to receive using SDL functions.