Emulate a mouse with the keyboard

Aug 19, 2011·
Julio Batista Silva
Julio Batista Silva
· 2 min read
blog Tutorial

As I already wrote in an earlier post, I bought a wireless keyboard and mouse so I could use the computer from about 3 metres away. But the mouse is not working.

While the combination of the Awesome window manager with mostly command-line programs means I almost never need a cursor, some programs and web pages are not very friendly to someone without a mouse.

Until I buy another mouse, my temporary solution was to set up the numeric keypad to control the pointer’s movement and to simulate the scroll wheel and the buttons.

  1. Download xdotool

    julio@julio-acer ~> sudo pacman -S xdotool
    
  2. Add the following lines to ~/.xbindkeysrc

    # Move northwest (up-left)
    "xdotool mousemove_relative -- -10 -10"
    KP_Home
    
    # Move north (up)
    "xdotool mousemove_relative 0 -10"
    KP_Up
    
    # Move northeast (up-right)
    "xdotool mousemove_relative 10 -10"
    KP_Prior
    
    # Move west (left)
    "xdotool mousemove_relative -- -10 0"
    KP_Left
    
    # Move east (right)
    "xdotool mousemove_relative 10 0"
    KP_Right
    
    # Move southwest (down-left)
    "xdotool mousemove_relative -- -10 10"
    KP_End
    
    # Move south (down)
    "xdotool mousemove_relative 0 10"
    KP_Down
    
    # Move southeast (down-right)
    "xdotool mousemove_relative 10 10"
    KP_Next
    
    # Left click
    "xdotool click 1"
    KP_Insert
    
    # Middle click
    "xdotool click 2"
    KP_Delete
    
    # Right click
    "xdotool click 3"
    KP_Enter
    
    # Scroll up
    "xdotool click 4"
    KP_Subtract
    
    # Scroll down
    "xdotool click 5"
    KP_Add
    

Adjust the keys to match the output of xev and that is it! I managed to simulate the mouse’s basic functions with the keyboard.

Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, mentoring, and contributing with code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus