Wednesday, March 14, 2012

Keyboard Layouts

Today I decided to tackle the problem of keyboard. I normally have two keyboard layouts, one for US English (the default) and one for Persian. I had heard that setxkbmap can help me do this. With some digging I found running this command can help me do that:
$ setxkbmap "pc+us+ir:2+inet(evdev)+group(shifts_toggle)"
I got that line by running setxkbmap -print on my Ubuntu machine. The output had something that seemed to be what I want. And I was right! Running that, I noticed that the changes I've made with xmodmap was reset. Well, all I had to do was to put the line that runs xmodmap after setxkbmap. Like this:
(run-shell-command "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\"")

;; load keyboard map
(run-shell-command "xmodmap ~/.xmodmap")
Except that didn't work. I logged out and logged back in, and I saw my alt and ctrl keys are not where they should be. A couple of times running those commands by C-x C-e from within Emacs gave me an idea what might be wrong. You see, by default, run-shell-command runs the command given to it asynchronously. I needed to make sure setxkbmap is done before I move onto xmodmap. Thankfully, run-shell-command has one last optional argument collect-output-p. It's function is to collect the program output, but in doing so it makes the execution synchronous. So adding that argument fixed the problem:
(run-shell-command "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\"" t)
Voila! Now I can switch layouts like before. Of course, I have no visual indication of the current layout anymore. I'm not 100% sure I want that. I might choose to work out a way for caps lock or scroll lock LED to do that for me later.

UPDATE: If you want a visual indicator for the current keyboard layout, you can change the setxkbmap invokation to something like this:
(run-shell-command
 "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\" -option grp_led:caps"
 t)
This makes the CAPS LOCK led to turn on when an alternate keyboard layout is active.

No comments:

Post a Comment

Better Console Font (in Arch)

In order to use Terminus as my console (tty) font, I simply installed the package terminus-font with pacman (includes both console and X11 ...