Keyboard Shortcut on Wake from suspend

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
mrguy314578
Level 1
Level 1
Posts: 1
Joined: Tue Feb 20, 2024 11:52 pm

Keyboard Shortcut on Wake from suspend

Post by mrguy314578 »

Hi Everyone,

I have a weird problem where the wifi/bluetooth card on my laptop cannot power on (hardblocked) after wake from suspend (i.e. closing laptop lid and reopening). This seems to be a somewhat common issue and I have tried every solution in every thread I've been able to find, all to no avail. The only solution I've found other than rebooting is pressing the airplane mode shortcut (FN+F10) twice after wake.

This is fine but I'm wondering if there is a way to run this keyboard shortcut automatically on wake. The catch is it seems to have to be the actual fn+f10 shortcut, as the following does not work:

Code: Select all

nmcli r all on
nmcli r all off
I'm hoping someone here can help as this goal seems to be pretty ungoogleable due to the number of people wanting to put their computers to sleep using a keyboard shortcut.

Thanks in advance, and if anyone has any ideas about actually solving the wifi card problem I'm very much open to that advice as well!
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Keyboard Shortcut on Wake from suspend

Post by 1000 »

1. You can add a ready-made script to autostart.
Depending on your desktop environment, you have a graphical application.
Or just put script there ~/.config/autostart
There may be differences depending on the graphical environment.
For example on mate I have *.desktop files where is link to script.
But I suppose the *.bash script will also work.
The graphical application just won't be able to see.

Doc if you need: https://specifications.freedesktop.org/ ... atest.html

2. Depending on your desktop environment, you can probably assign hotkeys in the settings.
Then you can use in bash script (for example) with xdotool. ( " man command " and examples are your friends )

3. Problems with falling asleep often occur because good cooperation hardware with ACPI is needed.
( sometimes there are other problems as well because it is still being developed )
- ACPI is supported by hardware and the kernel.
If you want read https://www.kernel.org/doc/html/latest/ ... index.html
If you want report to kernel
- https://korg.docs.kernel.org/bugzilla.html
- https://bugzilla.kernel.org/describecom ... oduct=ACPI
- If you want disable ACPI read https://help.ubuntu.com/community/BootOptions
( especially at the very bottom about GRUB and Kernel Options )
Warning: After disabling ACPI, you will not be able to control the functional buttons. ( they won't work )
e.g. to brighten the screen.

Edited
If I write nonsense, please correct me, because I haven't dealt with this topic for a long time.
And I could be wrong.

Edited
It may not be recommended for a beginner user, but maybe you will find something interesting on the websites
- https://wiki.ubuntu.com/DebuggingKernelSuspend
- https://wiki.archlinux.org/title/Power_ ... _hibernate
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Keyboard Shortcut on Wake from suspend

Post by 1000 »

AI is also your friend

1. Create a script that contains the keyboard shortcut you want to run. For example, you can create a shell script like `run_keyboard_shortcut.sh` that contains your desired keyboard shortcut command.

2. Make the script executable by running the following command: `chmod +x run_keyboard_shortcut.sh`.

3. Create a systemd service file that will run this script on wake. You can create a file like `keyboard_shortcut.service` in the `/etc/systemd/system/` directory with the following content:

Code: Select all

[Unit]
Description=Run keyboard shortcut on wake

[Service]
ExecStart=/path/to/run_keyboard_shortcut.sh
Restart=always
Type=simple

[Install]
WantedBy=multi-user.target

4. Enable the service to start on boot and start it with the following commands:

Code: Select all

sudo systemctl enable keyboard_shortcut.service
sudo systemctl start keyboard_shortcut.service

This setup should run your desired keyboard shortcut automatically on wake in Linux Mint. Make sure to replace `/path/to/run_keyboard_shortcut.sh` with the actual path to your script.
I can add with AI

Code: Select all

sudo apt-get install xdotool
example

Code: Select all

#!/bin/bash

# Send key press events for FN and F10 keys
xdotool keydown "Fn"
xdotool keydown "F10"
sleep 0.1  # adjust sleep time if necessary
xdotool keyup "F10"
xdotool keyup "Fn"
- first check if the script works

- then you can try add to systemd service or autostart and test.

- you can also test with a 5s delay ( add " sleep 5 " command at the beginning of the script ) if want work with systemd or wake up.

How it works on your laptop?
Fn+F10 - It only turns on Wi-Fi?
Fn+F10 - It only turns on and off Wi-Fi?

Edited
- And I forgot to mention. It is always worth adding messages to the script which will write to a file / log or system log.
For example you can add to script ( path you need edit )

Code: Select all

# Get the current date and time
current_date=$(date '+%Y-%m-%d %H:%M:%S')

# Log the date and time in an echo statement, then save to file
echo "$current_date : My script working" >>  /home/your_user_name/my_file.log
Then from this file / log you will sure when or if script worked.
Post Reply

Return to “Scripts & Bash”