Saturday, April 30, 2011

X keyboard binding

I have a tiny IOGear wireless keyboard. It's great: about 12" wide, 6" deep, with a little trackball. I can sit eight feet away from my big TV and work. When it's not needed, I can easily tuck it out of the way.

One big drawback: no native Home nor Page Up or Down keys. This is rather awkward: to scroll down a web page I always have to look carefully at the little blue "Fn" key on the left side of the keyboard, then carefully find the down arrow on the right side of the keyboard, then press and release them both. I'd rather not have to actually look while reading very important web pages, so I found a solution.

My window manager "wmii" uses the Super key to switch workspaces and move windows around. The Alt key is used by some apps. I'd like to have both Fn-Down and Super-Down map to PageDown, so it's not as awkward to scroll web pages.

To solve this, there are a lot of twisty little utilities, all slightly similar and different. Xev shows raw X events, and can tell you if your keys (like multimedia keys: Volume Up and so forth) are recognized by the raw X server. Xmodmap lets you remap events, like swapping CapsLock and Alt. Xbindkeys permits commands to run on key sequences, like pressing Alt-T to run a terminal. Xmacro lets you record and play back sequences of raw X events, including things like PageDown.

Lastly there's "xdotool", which sends key, mouse, or window manager events. For example, to scroll inside the current terminal, type:
xdotool key Shift+Page_Up

First version of ~/.xbindkeysrc:
"xdotool key Page_Up &"
  Mod4 + Up
"xdotool key Page_Down &"
  Mod4 + Down

This sort of works, but often fails: the window will flicker but not scroll. Second version searches for the topmost Chrome window, then sends the code to it. That doesn't work either...

To be continued.