Manual key code to Unicode conversion

dalhai wrote on Friday, April 03, 2015:

Hi guys,

First of all to the devs, thanks for your effort on GLFW, the library is amazing.


For my game, I want to provide users with the option to reassign key bindings from an option screen. My current approach is to store the key code mappings in an XML file, which I load when the game starts.


It would be nice to show users the symbols the respective keys produce instead of a physical key identifier. In the options menu, the user should be able to specify for an action, let’s call it “Jump”, a key that is supposed to trigger that action. However, instead of displaying

"Jump is triggered by <SOME_KEY>"

where <SOME_KEY> is a predefined name for the key code, I want to display the actual unicode output generated by the key. For example:

"Jump is triggered by ‘Ü’"

where ‘Ü’ would be the unicode output generated by pressing the specified key with the current layout.

Now, as far as I see, glfw internally translates the key code to unicode characters using the system specific methods. Is there any way for us to use these translations ourselves?


What I would do, given methods to convert key codes to unicode output:

  1. Load the action to key code mapping from the user preferences or a default configuration if the game is started for the first time.
  2. When in the key reassignment menu, translate the key codes to the corresponding unicode produced by the current keyboard layout and display them.

Is there anything like that provided by glfw, given that these translations are done internally anyway? If yes, and I just did not see it when looking for it, is there by any chance also a callback to react to keyboard layout changes?

If no, has anyone else implemented a platform independent solution for the problem which I could be using? Or is my only option to implement the functionality myself using the low level methods provided by each system if I do not want to stray away from glfw?

Thanks in advance for your help!

dougbinks wrote on Friday, April 03, 2015:

I did some work on keycode to text translation in this pull request but due to my error this has become somewhat confused, and I’m not sure what the current status is. You can see the basic API in this change - a new function glfwGetKeyName returns the name in UTF-8 for the key code given. Note that you might still store the key code in the file, so that when the user changes keyboard or language the key still exists (though some physical keys only exist on some keyboards/language combos).

I’d be happy to do any further work required for GLFW on this if elmindreda doesn’t already have a solution in the pipeline.

dalhai wrote on Friday, April 03, 2015:

I see. Thanks a lot, I’ll have a look at it!

Edit: Perfect, this is exactly what I was looking for, thanks again.

elmindreda wrote on Monday, April 06, 2015:

glfwGetKeyName is mostly done, based on Doug’s work, and will be included in 3.2.