3

My show Desktop icon/script doesn't work in the default Ubuntu 22.04 install, because it relies on xdotool to press WindowsD, and xdotool doesn't work in Wayland, and Ubuntu 22.04 comes with the Wayland window manager active by default, instead of X11. So, what tools can I use instead? Note: I know how to switch back to the X11 window manager now, but that's not my question. I want a tool that can press keys in Wayland.

I'm trying Python's pynput library too, but I can't make it work either. See my attempt here: Stack Overflow: pynput library not working as expected in Python to press Windows + D key.

Anyway, for this question I just want a tool that can replace xdotool and work in Wayland, and a demo to have it press WindowsD to show the desktop.

Note: If you are also experiencing bugs in Wayland, like me, I have a few fixes and show how to switch back to X11 in Ubuntu 22.04 in my other answer here.

5

1 Answer 1

3

I figured it out. Thank you very much to @Rinzwind for pointing out the existence of ydotool!

Here's how to have ydotool press Windows + D. It works in both X11 and Wayland!

I first wrote about this on my website here: Tutorial: Getting started with ydotool to automate key presses (or mouse movements) in Linux.

For full details, see my article. Here are the key parts, copied directly from my article above:

First, build and install it:

# install dependencies
sudo apt update
sudo apt install git cmake scdoc

# build ydotool
# See: https://github.com/ReimuNotMoe/ydotool#build
git clone https://github.com/ReimuNotMoe/ydotool.git
cd ydotool
mkdir build
cd build
cmake ..                 # takes < 1 second
time make -j "$(nproc)"  # takes < 1 second
sudo make install
# Note: the install command installs here:
#
#       -- Install configuration: ""
#       -- Installing: /usr/local/bin/ydotoold
#       -- Installing: /usr/local/bin/ydotool
#       -- Installing: /usr/lib/systemd/user/ydotool.service
#       -- Installing: /usr/local/share/man/man1/ydotool.1
#       -- Installing: /usr/local/share/man/man8/ydotoold.8

# See the man pages for help, and to prove to yourself it is installed
man ydotool   # for the main program
man ydotoold  # for the background daemon process
# help menu
ydotool -h    # for the main program
ydotoold -h   # for the background daemon process [VERY HELPFUL MENU!]

# check the version; my output is `v1.0.4-0-g57ba7d0`
ydotoold --version

Now, use it: have ydotool press Windows + D

# 1. start the `ydotoold` background daemon (`sudo -b` runs it in the
# background; see `sudo -h`).
# - It takes a couple seconds to fully start up and print "READY". Once it does
#   that, hit Enter a couple times to clear out the command line.
sudo -b ydotoold --socket-path="$HOME/.ydotool_socket" --socket-own="$(id -u):$(id -g)"

# 2. Use `ydotool`
# - Have ydotool press Windows + D once to hide all windows, then make it wait 2
#   seconds, then have it press Windows + D again to show all windows:
YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool key 125:1 32:1 32:0 125:0; \
    sleep 2; \
    YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool key 125:1 32:1 32:0 125:0

# 3. Additional commands you may need: to show, kill, and help

# See the `ydotoold` background running processes
ps auxf | grep ydotoold

# Kill the `ydotoold` background running processes
sudo pkill ydotoold

# View the `ydotool` main help menu, including a list of all sub-commands.
ydotool -h

# View the `ydotool` sub-command help menus. 
# - Bug ( https://github.com/ReimuNotMoe/ydotool/issues/206 ): the daemon *must*
#   be running first to see these help menus!
YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool key -h
YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool click -h
YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool mousemove -h
# etc. 

# Open the Linux C header file containing all available key codes that `ydotool`
# can press
gedit /usr/include/linux/input-event-codes.h

You can also see all possible key codes in this file online: https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h.

Again, for more details, see my website: Tutorial: Getting started with ydotool to automate key presses (or mouse movements) in Linux.

See also

  1. How can I add "Show desktop" to the GNOME dash or Ubuntu Dock?
    1. @AndAC's excellent wmctrl answer
    2. my spin on the wmctrl answer, relying on my eRCaGuy_dotfiles repo I have with scripts in it.
  2. Q&A: How can I install the latest ydotool 1.0.1 (keyboard automation tool working on Wayland) in Ubuntu 22.04?
3
  • 2
    It may be good to change the instructions to use the systemd service that comes with ydotool to manage the ydotoold daemon instead of manually launching the daemon using sudo -b in a terminal.
    – vanadium
    Aug 17, 2023 at 7:33
  • @vanadium, I only learned the basics of systemd (meaning: the systemctl command) hours ago, and don't know the instructions for ydotool. What are they? sudo systemctl start ydotool and sudo systemctl stop ydotool or something? Is that installed as part of sudo make install? Aug 17, 2023 at 7:36
  • 2
    It is outlined in the answer I posted about compiling ydotool: askubuntu.com/a/1413830/558158
    – vanadium
    Aug 17, 2023 at 10:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .