Skip to content
Advertisement

Get UTF-8 input with X11 Display

I’ve been trying and reading lots of resources on the internet, trying to find a way to get an UTF-8 keyboard (composed) input from a X Display. But I could not make it work.

I have tried the example code from this link (exaple 11-4), but no success.

I also have written a simple example (below) to try to make it work. My simple test case is to print an “é”, which happens by typing the acute and then the e.

What is wrong?

Thanks,

Here is my example:

JavaScript

Advertisement

Answer

You have to do this:

JavaScript

in your event loop. This runs the input method machinery, without it you will get raw X events. For example, when you press a dead accent key followed by a letter key, and do not call XFilterEvent, you will get two KeyPress events as usual. But if you do the call, you will get three events. There are two raw events, for which XFilterEvent(&ev, win) returns True. And then there is one event synthesized by the input method, for which XFilterEvent(&ev, win) returns False. It is this third event that contains the accented character.

If you want both raw events and those synthesized by the input method, you can of course do your own raw event processing instead of continue.

Note you will need buf[count] = 0; in order to print buf correctly (or explicitly use a length), Xutf8LookupString doesn’t null-terminate its output.

Finally, as mentioned in the comments, with recent versions of X11 you will need to specify a modify to XSetLocaleModifiers such as XSetLocaleModifiers("@im=none"), otherwise the extra events won’t be generated.

Here is a corrected version of the code:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement