Ensuring Natural-Scrolling Mouse and Touchpad Settings Are Set Correctly When Reconnecting Devices

Martin Thorsen Ranang
3 min readOct 27, 2020

I was confused when GNOME consistently failed to apply my natural-scrolling settings for my pointing devices if they were disconnected and then reconnected, and after my laptop resumed from sleep mode. In this article, I will show how I used the udev system to force GNOME to apply my preferences every time the USB device was reconnected. If you have similar problems, chances are good that you will be able to modify my scripts to match your environment.

Under GNOME in Ubuntu 20.04 you can configure “natural scrolling” for your mouse/trackball and touchpad independently. This is done in the “Settings > Mouse & Touchpad” menu, as shown in Figure 1.

Figure 1. In GNOME Settings, natural scrolling can be turned on in the Mouse & Touchpad menu.

I tried this and after a while got used to the natural scrolling. However, I got confused when after disconnecting and reconnecting my USB trackball, or after awakening my laptop from sleep mode, the scrolling had somehow been reset to “unnatural scrolling”.

Luckily, the udev dynamic device management system can be configured to mitigate this. The udev daemon receives device events whenever a device is added or removed from the system, and we can run a tailored script upon connecting, for example, a mouse or trackball.

To handle this, I wrote a udev rule in /etc/udev/rules.d/90-natural-scrolling-slimblade-trackball.rules, as shown in Listing 1.

Listing 1. The Udev rule that is triggered when the USB trackball is inserted.

The rule in Listing 1 states that when a device of model/product ID 2041 (Slimblade Trackball) from vendor with ID 047d (Kensington) the script natural_scrolling.sh should be ran from <username>‘s bin directory, as the <username> user. Naturally, you should replace the <username> with your own username. The vendor and product IDs are easily obtained, for example, by running lsusb looking for your USB connected device (the relevant IDs are shown in bold):

$ lsusb
[…]
Bus 001 Device 049: ID 047d:2041 Kensington SlimBlade Trackball
[…]

So, if you (most likely) have a different kind of USB pointing device, you should replace the idVendor and idProduct values in the above script with the appropriate ids from lsusb.

Finally, I wrote a small script — shown in Listing 2 — to reset GNOME’s natural-scroll mouse setting, in the file /home/<username>/bin/natural_scrolling.sh (again, you should replace <username> with your username); also, replace the uid value with your own user’s id (you can easily find it with the id) command.

Listing 2. The Bash script that applies the wanted natural-scroll settings.

The script is a bit hackish: I don’t like that it first has to turn natural-scroll explicitly off, before it is immediately turned back on again. Also, the script performs those two off/on commands in the background (notice the & at the end there). The fact that performing those gsettings commands in the background has proven necessary indicates that the real issue might be a timing/race condition between the kernel’s USB device initialization code and the user space commands issued by our script. In an earlier version of the script, the gsettings line started off with a sleep 1 && delay. And, on a more serious note: this script fixes the symptoms, not the cause of my problem: I do not know why GNOME does not simply remember or correctly apply my settings even after a device is reconnected.

The above scripts can easily be modified to handle touch pad settings instead; the gsettings command should then be modified to gsettings get org.gnome.desktop.peripherals.touchpad natural-scroll [false/true].

I hope that this might help you if you have encountered the same problem as me — with GNOME forgetting your natural-scrolling settings after reconnecting a USB device, or when your laptop has resumed from a suspended state.

--

--

Martin Thorsen Ranang

I dream in code, love skateboarding and spending time with my wife and three children. I have a Ph.D in Computer Science.