Emulate mouse click when hitting a key

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"

3

2 Answers

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
2

Most desktops have some accessibility settings that will control the mouse with the keyboard already built-in, like XFCE has them in it's All Settings -> Accessibility -> Mouse -> Mouse Emulation, that uses the keyboard number pad so the 5 key does exactly what you're trying to do.

If I recall, gnome, cinnamon and mate have similar features under something like "Accessibility" or assistive technology, etc. Or an installable program/package like mousetweaks ("mouse accessibility enhancements for the GNOME desktop") or keynav (but I couldn't get it to work on xfce).

Or, if your touchpad still works ok (just not the buttons) you could just do a tap for a single click, a tap & then hold for a click & drag, a double-tap = double click... and if they don't work there should be a line or two in the terminal that should get those to work (don't remember them offhand now). I wouldn't want to write my own script when there are a few other options that should work

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like