Ubuntu – How to make shortcut keys move the mouse, as an alternative to the touchpad

gnomekeyboardmousescriptsshortcut-keys

Due to large quantities of Chocolate Cake falling on my computer's touchpad it has become unresponsive at times (though the left- and right-click works fine) and I would like to make it so that when I press CTRL and at the same time hold down one or two of the arrow keys for the mouse to move accordingly. So if the up-arrow key is pressed it will move up, if the left-arrow key, then left, and so, and if the right-arrow key and the top-arrow keys are being help down together for it to move diagonally (and so on for the rest of the keys).

I am running Ubuntu GNOME 15.10 with GNOME 3.18, can this be done? And if so how? I would accept a script which does this.

Best Answer

  • Scripted alternative to Universal access (to move the mouse)

    Not 100% exactly like you describe, nevertheless an alternative for using the mouse or touchpad. The answer below in fact is an almost exact copy of an answer I posted a long time ago on this question. I deleted it however, since I never had the chance to find out if it was what OP was looking for (no response at all).

    In your case, since you only want to use it to move the mouse, you can leave out a number of shortcut keys (the last three) which will also prevent the most "risky" options. The scripted options should work fine any way, without risks on conflicting situations.

    Mouse Keys alternative

    As mentioned, the script offers basic mouse functionality, as an alternative to the default Mouse Keys- option in System Settings > Universal Access. Unlike Mouse Keys, the mouse pointer is moved (either x- or y wise) by repeated clicks, in two modes:

    • bigger steps (as it is set now: 50px)
    • smaller steps to "finish" (as it is set now: 3 px)

    To my own surprise, I found it reasonably comfortable to use. It is definitely not the same level as a "real" mouse, but (much) more comfortable than the use of Mouse Keys, at least in my experience. After five minutes to get used to it, I was able to perform practically all "normal" actions; opening applications, drag windows, edit text etc.

    Disadvantage

    There is however a downside to mention:

    Unlike Mouse Keys, the setup does not "claim" the NumPad exclusively. The disadvantage of that is that there is a bigger chance of key clashes, especially with the left/right click and hold the left click -functionality. Although I did not run into insurmountable situations, you might run into some conflicts, and being familiar with general shortcut keys is an advantage.
    You will have to try and see if it works for you.

    The setup

    1. The script needs xdotool:

      sudo apt-get install xdotool
      
    2. Disable mouse keys (most likely it already is)

    3. Copy the script below into an empty file, save it as mouse(no extension) in ~/bin (create the directory if needed).

      #!/usr/bin/env python3
      import subprocess
      import sys
      #--- Don't change these lines:
      direction = sys.argv[1]
      speed = sys.argv[2]
      #--- Change the steps below if you'd like to
      big_step = 50
      small_step = 3
      #---
      
      step = big_step if speed == "fast" else small_step
      step = -step if direction == "left" or direction == "up" else step
      get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
      
      def move():
          coords = [int(it.split(":")[1]) for it in get(["xdotool", "getmouselocation"]).split()[:2]]
          if direction in ["left", "right"]:
              cmd = "xdotool", "mousemove", str(coords[0]+step), str(coords[1])
          elif direction in ["up", "down"]:
              cmd = "xdotool", "mousemove", str(coords[0]), str(coords[1]+step)
          subprocess.call(cmd)
      move()
      
    4. Make the script executable.

    5. If you just created ~/bin, either log out/in, or run source ~/.profile, to make the directory "visible" in $PATH.
    6. Now add a number of shortcut keys. All shortcut keys as I set it up are aiming on the use of the NumPad, in combination with Shift+Ctrl (fast move), Ctrl (slow move, mouse left click), Ctrl+Super, (right click, mouse down).

      Add the following shortcut keys: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the commands (11 in total, including mouse keys):

      mouse left fast 
      

      key combination: Shift+Ctrl+Num Left

      mouse right fast    
      

      key combination: Shift+Ctrl+Num Right

      mouse up fast       
      

      key combination: Shift+Ctrl+Num Up

      mouse down fast     
      

      key combination: Shift+Ctrl+Num Down

      And:

      mouse left slow 
      

      key combination: Ctrl+Num Left

      mouse right slow 
      

      key combination: Ctrl+Num Right

      mouse up slow       
      

      key combination: Ctrl+Num Up

      mouse down slow     
      

      key combination: Ctrl+Num Down

      Additionally, I set shortcut keys for:

      • left-click:

        xdotool click 1 
        

      key combination: Ctrl+*

      • right-click:

        xdotool click 3 
        

      key combination: Ctrl+Super+*)

      • hold left click (e.g. to grab, use click to release):

        xdotool mousedown 1 
        

      key combination: Ctrl+Super+/)