Gentoo Archives: gentoo-user

From: Gevisz <gevisz@×××××.com>
To: gentoo-user@l.g.o
Subject: [gentoo-user] A direct keyboard layout switch for xfce4
Date: Sun, 11 Oct 2015 09:17:04
Message-Id: 561a28ff.4a7e700a.a8ac.6d6d@mx.google.com
I am asking this question here after discussing it
at vim_use mailing list (as an slight off topic to
the "vim and touch typing" thread) and after an extensive
google search earlier.

Below is some resumee from that discussion but for those
of you how do not want to read all that I just formulate
my question in short first: "Is there any plugin or
application working under xfce4 that allows switching
the keyboard layout directly, that is not by circling or
toggling it.

By a direct keyboard switch I mean the way to switch 
to a certain keyboard layout irrespectively of the its
initial state. For example, I need a (configurable)
hot key to switch to a Cyrillic keyboard layout if
I am currently in the English keyboard layout and
to do nothing if I am already in the Cyrillic keyboard
layout.

As far as I know it is impossible to do with the current
X config and/or xfce4 keyboard layout switch plugin.

So, my original post at vim_use mailing list was as follows:

> I do use a "blind typing" in the sense that > I do not look into the monitor when I type, > only to the keyboard. I get used to this style > yet about 25 years ago trying to save my eyes > from the ray monitors of that days. > > Usually, I type a whole sentence without looking > into the monitor. > > It does not mean that I do not know my keyboard layout. > I do know it and can type in a complete darkness > (but much more slowly, of course). > > My hands move over the keyboard almost automatically > but I still need a little feedback from my eyes to not > hit "i" instead of "o" for example. > > In this connection I have only one inconvenience > connected with the facts that > 1) I usually have to use 3 keyboard layouts at > the same time switching between them with a hot key, > 2) it is impossible to have a "direct hot key switch" > to a certain keyboard layout in Linux world. > > Because of that I have to remember all the time not only > in which vim mode I am but also in which keyboard layout > I am and it is too much for me. As the result, I too often > end up raising my eyes to the monitor and finding out that > I have typed the whole sentence in incorrect keybord layout. :( > > In such times I very much miss the good old MS DOS keyrus > driver that could have been configured to produce a pleasant > "crimping" noise when typing in a Cyrillic keyboard layout > and nothing when typing in Latin keyboard layout, for example. > > But the "direct hot key switch" could also help a lot in this > situation. Unfortunately, as far as I know, it is impossible > in the Linux World. > > P.S. By a "direct hot key switch" I mean the hot key that > switches directly to a certain keyboard layout, not by > circling through all the active ones. > > However, such a "direct hot key switch" is possible in Windows.
I was encoraged to ask this question by the post of Nikolay Pavlov who wrote:
> I would suggest to ask this on superuser.com. AFAIK you cannot > do this directly with X server configuration, but there are a number > of “external” keyboard switchers which may have the needed capabilities. > The other variant is a console app like setxkbmap or xkb-switch used > in conjunction with global shortcuts capabilities provided by your > DE/WM (if no, there still are apps which provide global shortcuts > using X server own capabilities).
One of the most valuable suggestions were provided by Erik Christiansen. Below, I provide them after the ">" with my comments intermitted.
> It is very risky to say that anything is impossible in linux;
Ok, next to impossible. ;)
> it usually turns out that there are at least three ways to do what > one at first thinks impossible. My first reaction to your problem was > to suggest several files to switch to ~/.Xmodmap, and then find out > if a SIGHUP to X would make it re-read its config.
And what if not? I may try this solution but I still have to google how to send a SIGNUP signal to X and, moreover, to find out how to send it in a quick and convenient way. Otherwise this way will not be a solution at all. Moreover, even before trying it, I suspect that this way of achieving a "direct hot key keyboard switch" will cause X to re-read its configuration from a file on a disk that can take a noticeable time, which is not desired. The true solution to this should be to keep all the needed keyboard layouts in memory and only change the pointer to the corresponding memory locations by a corresponding hot key. I believe that such a solution should be provided in the X configuration and realized in the corresponding window manager plugin but, as far as I know, it is not. That is why I wrote that it is impossible in Linux (unless you can re-write X, of course :).
> Much easier, by appearances, is Tony's suggestion. A quick look at > ":h keymap", leading to ":h mbyte-keymap", looks very promising, not > least since there are many ready-to-use mappings: > > $ locate -r 'keymap.*vim' | wc -l > 130 > > That, though, only maps in Vim, and only during text insertion. > (which may be just what you want)
Yes, you are right, this workaround can be what I actually need. Moreover, I have used a similar workaround when I used emacs about 15 years ago. However, in ":h mbyte-keymap" is written that this workaround would interfere with using "commands that take a single character argument, like f and r" what is definitely a drawback. Moreover, as far as I understand, in this workaround the keymap tables are read anew from disk every time you hit <C-^>, so it, again, can take a noticeable time, what is not good. Otherwise, this suggestion looks interesting and I will try it a bit later.
> If cross-application mapping is desired, though, then an alternative to > ~/.Xmodmap is to just execute (several of) something like: > > $ xmodmap -e 'keycode 91 = 0x2c' # Change numeric pad '.' to ',' > > in a shell script, after using xev to elucidate the keycode and keysim of > all the keys you want to remap. > > For me, this reverts the above mapping: > > $ xmodmap -e 'keycode 91 = 0x2e' # Change numeric pad ',' to '.'
Thank you for pointing me to the xmodmap and xev utilities. I will consider this solution as well but it would lead to execution of a long remapping script that again should take a noticeable time comparing with just changing a pointer to the corresponding keymap table in memory.