Thursday, January 16, 2014

ratpoison and xdotool: How to Move the Mouse by Using the Keyboard in Linux

We interrupt this blog for an important announcement: With ratpoison, you can use the mouse using keyboard bindings!

For those of you who don't know what ratpoison is, it's a window manager for linux, like fluxbox, GNOME, or KDE. It's designed to be able to work without a mouse (hence the name). Using ratpoison, I have found a way to use the pointer without actually using the mouse.

Here is my .ratpoisonrc file:
startup_message off
set font Mono-10
set bargravity nw
set bgcolor cyan
exec rpws -i
exec /usr/bin/rpws init 4 -k
source .ratpoisonkeys
source .ratpoisonmouse
set winname class

set border 0

And here is my .ratpoisonmouse file:
bind C-j ratclick 1
bind C-k ratclick 2
bind C-l ratclick 3
definekey top M-j ratrelwarp 0 16
definekey top M-k ratrelwarp 0 -16
definekey top M-h ratrelwarp -16 0
definekey top M-l ratrelwarp 16 0
definekey top M-y ratrelwarp -16 -16
definekey top M-u ratrelwarp 16 -16
definekey top M-b ratrelwarp -16 16
definekey top M-n ratrelwarp 16 16

definekey top M-J ratrelwarp 0 256
definekey top M-K ratrelwarp 0 -256
definekey top M-H ratrelwarp -256 0
definekey top M-L ratrelwarp 256 0
definekey top M-Y ratrelwarp -256 -256
definekey top M-U ratrelwarp 256 -256
definekey top M-B ratrelwarp -256 256
definekey top M-N ratrelwarp 256 256

definekey top F10 ratclick 1
definekey top F11 ratclick 2
definekey top F12 ratclick 3
definekey top M-i rathold down 1
definekey top M-o rathold down 2
definekey top M-p rathold down 3
definekey top M-I rathold up 1
definekey top M-O rathold up 2
definekey top M-P rathold up 3

The entries in the above file mean that if I type ALT+H,J,K, or L, I can move the mouse left, down, up or right. F10 clicks on stuff, and F12 right-clicks on stuff. The rest can be extrapolated, I think, by looking at the .ratpoisonmouse file above.

Don't wanna switch to ratpoison? You can do the same thing in any other linux window manager using xdotool. It's a commandline program that lets you move the mouse around, among other things. By binding keystrokes to different xdotool commands, you can get the mouse to move and click.

For example, here is what could be an excerpt from .fluxbox/keys, the fluxbox keybindings file to do move the mouse up, down, left and right as explained above, plus click the mouse:

Mod1 j :Exec "xdotool mousemove_relative -- 0 16"
Mod1 k :Exec "xdotool mousemove_relative -- 0 -16"
Mod1 h :Exec "xdotool mousemove_relative -- -16 0"
Mod1 l :Exec "xdotool mousemove_relative -- 16 0"
F10 :Exec "xdotool click 1"
F11 :Exec "xdotool click 2"
F12 :Exec "xdotool click 3"

Cool, huh?