1

I like to use middle mouse button emulation, because my middle mouse button is a scroll wheel and it takes a lot of pressure to register a click. This gets physically painful pretty quickly.

I find it much easier to just click the left and right mouse buttons simultaneously. And I use this feature all the time.

I have a script in ~/scripts/mouse.sh that does this:

#!/bin/bash

# Enable middle button emulation
# from https://askubuntu.com/a/201825/54278
if [[ -n ${DISPLAY} ]]; then
    pointer1="MX Master"
    id1=$(xinput | awk -F= "/$pointer1.*pointer/ {print \$2}" | cut -f1)
    xinput set-prop "${id1}" "libinput Middle Emulation Enabled" 1
fi

This works well, but I have to run it manually every time I reboot.

I've created ~/.config/autostart/mouse.sh.desktop. These are the contents:

[Desktop Entry]
Type=Application
Exec=/home/david/.scripts/mouse.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_AU]=Mouse
Name=Mouse
Comment[en_AU]=Middle button emulation
Comment=Middle button emulation

My problem is that my script doesn't do anything on login.

I still have to open a terminal and run my script before middle mouse button emulation works.

I've tried removing the if [[ -n ${DISPLAY} ]]; then condition, and I've tried adding a sleep at the start of the script.

I've also tried adding the contents of my script to ~/.profile.

None of these things have worked. This has been bugging me for years!

Thanks for having a look :-)


Edits

  1. Also tried Exec=/bin/bash /home/david/.scripts/mouse.sh. Thanks @PRATAP
  2. Ubuntu 19.04, though it also hasn't worked in the last few versions, including 18.04
  3. Tried deleting the .desktop file and using the Startup Applications GUI
  4. Tried removing the if [[ -n ${DISPLAY} ]]; then condition
  5. I had a flash of inspiration and tried using Exec=/usr/bin/xterm -e /home/david/.scripts/mouse.sh - also no luck
  • 2
    what is your OS? I am on Ubuntu 18.04 and I have kept this command in Startup Applications Preferences /bin/bash /home/user/scripts/mouse.sh and it is working well.. i.stack.imgur.com/SzV1c.jpg – PRATAP Jun 10 at 5:14
  • 1
    I think that was it! I added /bin/bash before the script and it seems to be working now - thanks! Kind of can't believe something so simple annoyed me for so long! – david_nash Jun 10 at 6:53
  • I jumped the gun - I rebooted this morning and back to not working. Argh! – david_nash Jun 10 at 22:40
  • Sorry - Ubuntu 19.04. But it hasn't worked in the last several versions either, including 18.04. Startup application preferences saves its data in ~/.config/autostart, so if I use the GUI it shows the same /bin/bash /home/david/.scripts/mouse.sh in the command field – david_nash Jun 10 at 23:16
  • Let us continue this discussion in chat. – david_nash Jun 10 at 23:19
1

The thing worked for me is the below script

/home/user/mouse.sh

#!/bin/bash

    pointer1="Logitech USB Receiver Mouse"
    id1=$(xinput | awk -F= "/$pointer1.*pointer/ {print \$2}" | cut -f1)
    xinput set-prop "${id1}" "libinput Middle Emulation Enabled" 1

some of the Output of xinput

$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ ETPS/2 Elantech Touchpad                  id=11   [slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver Consumer Control    id=16   [slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver Mouse               id=18   [slave  pointer  (2)]

and added the command /bin/bash /home/user/mouse.sh in Startup Applications Preferences..

enter image description here

  • Thanks for this, it's very clear. I've followed the instructions here and it seems to be working after a reboot. I'll see if it works after another reboot or two – david_nash Jun 11 at 7:50
  • Just rebooted this morning after applying an update - middle click emulation isn't working again - this is so frustrating! – david_nash Jun 11 at 21:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.