1

I am using Ubuntu 22.04 on a Lenovo ThinkPad P16 Gen 1.

At the bottom of my touchpad are left, middle and right click buttons. I am often accidentally pasting things by clicking the bottom middle of the pad. To avoid this, I can change the middle-click button to a left-click by:

xinput set-button-map 12 1 1 3 4 5 6 7

But when I restart my machine this resets and so I have to do this again each time - and as I understand it, the xinput for my touchpad won't be 12 every time (as it is in this example).

How can I make this change permanent or automate this mapping change each time I boot?

  • You cannot "make it permanent". You will have to re-do the setting every time you start your windowing system. Look at your ~/.config/autostart/ directory. – waltinator Jan 02 '24 at 16:46

3 Answers3

2

How can I make this change permanent or automate this mapping change each time I boot?

The easiest way is to create a new Startup Application for that and use this:

sh -c 'sleep 5 && xinput set-button-map 12 1 1 3 4 5 6 7'

... in the Command: field similar to this example ... and it will be automatically executed on reboot and on login.

Notice that sleep 5 will delay the execution of the command for five seconds (increase it if needed) to allow the graphical user session to fully load before xinput is executed ... This is needed because xinput requires a working graphical user session to execute and do it's job.

and as I understand it, the xinput for my touchpad won't be 12 every time

That is possible if the device is e.g. removable and is connected to a different port the next time it's plugged in again.

While the ID might change, the device name usually doesn't ... So, you could look into the output of xinput list to get the name of your device and use that to get the ID like so:

xinput list --id-only "Exact Device Name" 2>/dev/null

... or even like so:

xinput list 2>/dev/null | grep --color=never -Po "Exact Device Name.*=\K[0-9]+"

... and that can be used in the above sh command string in place of the device ID (in a command substitution syntax $(...)) like so:

sh -c 'sleep 5 && xinput set-button-map "$(xinput list --id-only "Exact Device Name" 2>/dev/null)" 1 1 3 4 5 6 7'

or like so:

sh -c 'sleep 5 && xinput set-button-map "$(xinput list 2>/dev/null | grep --color=never -Po "Exact Device Name.*=\K[0-9]+")" 1 1 3 4 5 6 7'

... and your device ID should be automatically discovered.

Raffa
  • 32,237
0

If you edit your .profile startup file you can execute this at startup.

.profile is located in the /home/{user} directory.

You can create the .profile file if it does not exist.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
# umask 022

if running bash

if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi

set PATH so it includes user's private bin if it exists

if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi

set PATH so it includes user's private bin if it exists

if [ -d "$HOME/.local/bin" ] ; then PATH="$HOME/.local/bin:$PATH" fi

sh -c 'sleep 5 && xinput set-button-map 12 1 1 3 4 5 6 7'

Jpirok
  • 1
0

Find out the exact name of the device:

xinput list

terminal1

xinput list | grep "TM3127-001"

You can't rely on "Touchpad", "TouchPad", etc. I know this will never change.

xinput list | grep "TM3127-001" | grep -Eo 'id=[0-9]{1,2}'

Isolate the id number (above), then grep out the numbers (below):

xinput list | grep "TM3127-001" | grep -Eo 'id=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'

terminal2

sudo nano /path/to/script/my_script.sh

Use the following script for my_script.sh.
Replace YOUR_UNIQUE_STRING with your device name, where mine was TM3127-001.
Save & exit.

#!/bin/bash

device_id=$(xinput list | grep "YOUR_UNIQUE_STRING" | grep -Eo 'id=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}')

xinput set-button-map "$device_id" 1 1 3 4 5 6 7

exit 0

Change mode to executable: sudo chmod +x /path/to/script/my_script.sh

sudo nano /etc/rc.local

Put the following at the end of the file:

/path/to/script/my_script.sh start

Now save, exit & reboot. Your button mapping is now permanent.