2

I'm a dvorak touch typist. On my machines, dvorak is default; on other machines, I regularly use setxkbmap (when using X) or loadkeys (otherwise). But what about "shared" systems?

When working on systems e.g. being readied for a customer (Linux, mostly RH-based), we sometimes share the admin's account. And if I forget to switch back after doing my part … all hell breaks loose. Understandably so, since nobody can type anything anymore!

I was thinking about starting an at-job at the beginning of my work session; but the system(s) in question don't have an atd running (and I cannot change that). :-(

Ideally, I'd like to open a terminal, switch keyboard layout, and have the system "forget" the change when I close the terminal. Both setxkbmap and loadkeys on the other hand change the layout on a "higher" level.

Does anybody have an idea?
(It's OK that I have to type extra commands and stuff, it's just that I cannot assume others are willing to.)

1

The easy way is to log out after you're done. Your personal changes only last until you log out. That being said, intervening on someone's account is a common use case.

Type/paste this into your terminal (running bash or zsh):

xkb=$(xkbcomp :0 -); trap 'xkbcomp - :0 <<<"$xkb"' 0 1 2 15; setxkbmap dvorak

If you either exit the shell normally (exit, Ctrl+D) or close the terminal normally (which will send a SIGHUP), i.e. under normal ways to exit the shell session, the trap will restore the original keymap.

Don't do this in multiple terminals at the same time: the first one to exit would restore the original configuration, and the last one to exit would restore whichever configuration was in place when you ran this command.

If you know what keymap to restore and only want to handle normal terminal closure, you can simplify this to e.g.

trap 'setxkbmap us' 0 1; setxkbmap dvorak

Beware that this doesn't restore customizations that people may have made through a typical keyboard settings GUI configurator! (Which is essentially options present in standard XKB maps.) If you want to restore the user's customizations, you need to save the original keymap with xkbcomp :0 - as above.

Alternatively, use an interface that provides per-window switching layout, and stick to the one terminal window (remember not to launch any GUI application). Some desktop environments provide this feature (e.g. XFCE), as does kbdd. Kbdd is unlikely to be available, let alone configured, on a default installation, but maybe your default desktop environment has that feature.

A few remarks that don't apply to your use case, but apply to the use case of intervening on a user's machine while logged into their account:

  • This can introduce subtle differences in layouts that were originally set by xmodmap, but people who rely on such subtleties would know how to restore their hand-crafted keymap from a layout where the basics work.
  • I'm not sure if the current active level is restored correctly in multi-level (i.e. multi-language) configuration, but again, people who routinely switch between layouts would just use their usual method to switch layouts.
  • This may interfere badly with layout switching provided through a background application that changes the XKB layout (as opposed to changing levels). In such a case, you should ask them to switch to Dvorak through their layout-changing method.
  • I'll probably simplify your solution a bit to fit my case (i.e., so that I can remember the command by heart ;-) ), but this is definitely a solution to my problem. Thanks! – NicApicella Oct 26 '17 at 7:30
  • @NicApicella I've added a simplified solution, but that simpler solution doesn't restore the keyboard configuration correctly, it just restores some specific keymap. – Gilles Oct 26 '17 at 19:19
0

Setup .profile or similar to

(1) Test if X is running ($DISPLAY).

(2) Save current keyboard mapping to some file (setxkbmap -print for XKB, or modmap -pke for X, dumpkeys for console)

(3) Set your keyboard map.

(4) Restore old keyboard map when the shell exits, see e.g. here.

That won't work with multiple terminals, you'll have to use some "locking" file to determine when the first terminal starts the shell, and the last exits.

0

I wonder if there is a hardware-oriented solution. setxkbmap takes a -device option to only affect a given keyboard, where there are several, instead of the core X11 device.

So perhaps if you were to carry a real dvorak keyboard around with you, and plug it in, keeping the original keyboard too, you could run a setxkbmap for it that would simply become irrelevant when you unplugged your keyboard. Use xinput -list to get the id of each device. (It doesn't have to be a real dvorak keyboard, just another keyboard.) An advantage is that two people can each type on their own keyboard and there is no conflict of mapping.

You might experiment with a single keyboard: if you explicitly use the id of that keyboard, instead of the default core, then when you unplug it and plug it back in, does it lose your configuration change?

  • That's a good point, actually—makes sense that the layout is bound to the keyboard. Unfortunately, it's not going to be of much help in my case: Most of the machines I mentioned in my questions are virtual ones… (Or maybe…let me check!) – NicApicella Oct 26 '17 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.