Why does "Airplane Mode" keep toggling on my HP laptop in Ubuntu 18.04?
In the gnome shell, "Airplane Mode" is automatically activated on boot for many HP laptops, when the screen is tilted sideways, or when the lid is opened/closed. The following is a fix for HP laptops running Linux and using the gnome shell. Keycode 240 is defined as KEY_UNKNOWN (a kind of no-op key) in /usr/include/linux/input-event-codes.h. Also your syslog will no longer prompt you to define the HP e057 and e058 codes, which can be remarkably annoying.
First is a fix using a classic SysV init script, placed in the init.d directory and symlinked accordingly (Basically, we want it to run regardless of runlevel on anything except halt (0) or reboot (6)). Second is a systemd service that is far more reliable and perhaps present with recent systemd changes, which also happens to work on Fedora 28 and other distribution platforms. On Fedora in particular, you will find an almost complete absence of scripts in the /etc/init.d directory, along with a little README detailing the transition from SysV to systemd. The first method will work, but the second method is much more "future friendly" especially considering where Ubuntu is heading, and will also work in a slightly less...annoying way. Systemd init scripts are actually quite handy, and fully implemented and used frequently in Ubuntu 18.04.
Old SysV method:
$ sudo sh -c 'printf "#!/bin/sh\n/usr/bin/setkeycodes e057 240 e058 240\n" > /etc/init.d/hp-keycodes' $ sudo chmod +x /etc/init.d/hp-keycodes $ sudo ln -s /etc/init.d/hp-keycodes /etc/rc1.d/K01hp-keycodes $ sudo ln -s /etc/init.d/hp-keycodes /etc/rc2.d/S01hp-keycodes $ sudo ln -s /etc/init.d/hp-keycodes /etc/rc3.d/S01hp-keycodes $ sudo ln -s /etc/init.d/hp-keycodes /etc/rc4.d/S01hp-keycodes $ sudo ln -s /etc/init.d/hp-keycodes /etc/rc5.d/S01hp-keycodes $ sudo reboot
New Recommended Systemd Method:
$ sudo nano /etc/systemd/system/hp-keycodes.service
Paste the following lines or type them into the file:
[Unit] Description=HP setkeycodes fix [Service] Type=oneshot Restart=no RemainAfterExit=no ExecStart=/usr/bin/setkeycodes e057 240 e058 240 [Install] WantedBy=rescue.target WantedBy=multi-user.target WantedBy=graphical.target
Save and confirm the filename with ctrl-x, y.
$ sudo systemctl daemon-reload $ sudo systemctl enable hp-keycodes.service $ sudo reboot
And that's it, no more annoying weird stuff when you tilt your screen or close/open the laptop lid!