Ubuntu – Emulate mouse click when hitting a key

bashkeyboardmouseshortcut-keystouchpad

I am trying to emulate left-mouse-down when Home key is down and left-mouse-up when Home key is released.

The reason behind that is my touchpad's buttons are terrible and I like to use the track-point (red nipple) with left mouse click for highlight (copy) text. Instead of using the touchpad's button I want to use the 'Home' key. I am able to emulate the mouse-down part but I don't know how to release the click when the 'Home' key is released.

Here is what I have so for. first I use the xbindkeys tool. I added this entry to the .xbindkeysrc:

"~/bin/mouse-click"
  m:0x0 + c:110

mouse-click is this bash script:

#!/bin/bash
xdotool mousedown 1

The problem is the click is never being released so I need to find a way to detect the release of the 'Home'. Here is the script I am trying to write. The comments describe what I am trying to do:

#!/bin/bash

# not sure how to do that:
exit if Home key is in a pressed state (to avoid more mousedown actions)

xdotool mousedown 1

# not sure how to do that: (if Home key is released, release the mouse click)
while xinput query-state <device name> | grep -Flq 'key[9]=up'
xdotool mouseup 1

So That's what I am trying to figure out:

  1. Query the state of 'Home' key (pressed or not).
  2. Listen to keyboard events and detect a release of 'Home' key.

If there are better approach to achieve that, I would love to hear about them.
Thanks!

Edit: the solution is one line: xmodmap -e "keycode 115 = Pointer_Button2"
so now my Home key is doing left click and behave just as if I hit the mouse click and I can keep the key pressed and it highlights (copy) as expected.

I also mapped my End key to be middle click, so I can use it to paste:
xmodmap -e "keycode 110 = Pointer_Button1"

Best Answer

The solution is

xmodmap -e "keycode 115 = Pointer_Button2"

Now my Home key is doing left click and behaves just as if I hit the mouse click and I can keep the key pressed and it highlights (copies) as expected.

I also mapped my End key to be middle click, so I can use it to paste:

xmodmap -e "keycode 110 = Pointer_Button1"

Note that it is necessary to have "mousekeys" (mouse emulation using the numeric keypad) enabled for this to work. "mousekeys" can be toggled by pressing Shift+NumLock or set explicitly by running:

dconf write /org/gnome/desktop/a11y/keyboard/mousekeys-enable true
dconf write /org/gnome/desktop/a11y/keyboard/mousekeys-enable false