<-- back RSS Feed

Setting up a keyboard to use as a shortcut board in emacs.

This is a pretty simple hack you can use any keyboard for to have it on the side for commands you use often enough to warrent a keypress but still find it inconvinient to press C-x * or whatever to do. I like to have it on the right of my main keyboard for me to quickly reach over to when I need to. I haven't tested this on a Mac but I'd assume there is a similar process. On windows there is this video from Taran of LinusTechTips though I have not tried this.

Setting the keyboard layout

To do this you need 2 commands xinput and setxkbmap. First run xinput and the output should look something like this:


yur3i :: [~] :: [01:25 am]
$ xinput
 Virtual core pointer                          id=2    [master pointer  (3)]
    Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
    USB Keyboard                              id=11   [slave  pointer  (2)]
    SynPS/2 Synaptics TouchPad                id=13   [slave  pointer  (2)]
    TPPS/2 IBM TrackPoint                     id=14   [slave  pointer  (2)]
 Virtual core keyboard                         id=3    [master keyboard (2)]
     Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
     Power Button                              id=6    [slave  keyboard (3)]
     Video Bus                                 id=7    [slave  keyboard (3)]
     Sleep Button                              id=8    [slave  keyboard (3)]
     Dell Dell USB Keyboard                    id=9    [slave  keyboard (3)]
     USB Keyboard                              id=10   [slave  keyboard (3)]
     AT Translated Set 2 keyboard              id=12   [slave  keyboard (3)]
     ThinkPad Extra Buttons                    id=15   [slave  keyboard (3)]
     USB Keyboard                              id=16   [slave  keyboard (3)]
yur3i :: [~] :: [01:25 am]
$
    
This displays every input device on your system. As I was using a dell keyboard I was able to easily identify that the keyboard I was after is 'Dell Dell USB Keyboard', note the id number (in my case '9') and run

	setxkbmap -device 9 -layout ru
    
This sets the keymap of only keyboard 9 to Russian. Obviously if you need russian characters you will want to set it to a layout where you do not need whatever symbols the alphanumeric cluster types.

Emacs keybindings

Now that the keyboard is configured to be in a Russian layout we can configure emacs to run functions as any other keybinding except using the keys on our new keyboard.

	(global-set-key (kbd "я") (lambda()
				    (interactive)
				    (find-file "~/.emacs.d/init.el")))
    
This binds the z key to open my config file. I like to put some masking tape on the keys and write a label to help me remember. I like to have things like various commonly used files, magit commands, enabling visual line mode and others on this keyboard for examples of what I use it for.

Persisting through reboots

Now this is all well and good for that session but once you reboot you have to find the id of your keyboard and change the layout again. I wrote a regular expression for finding the id of my keyboard (theres probably a better expression but im not the best at them) and wrapped it in a small shell script to automatically set the keyboard up if it is plugged in.

id=$(xinput | grep -Eo "Dell Dell USB Keyboard\s+id=[0-9]{1,4}" | cut -c 45-)

if [ "$id" != "" ]
then
    setxkbmap -device $id -layout ru
fi
    
I set this to start with my window manager seeing as it seemed to break when I ran it at startx time. Then you are done! Thanks for reading. Many thanks to the people of this thread for helping point me in the right direction with this project.