Score:2

How can I write a program to press keys, such as Windows + D, in Wayland? (replace `xdotool` in Wayland)

cn flag

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.

cn flag
https://askubuntu.com/questions/956640/equivalent-to-xdotool-for-wayland states 2 alternatives
Gabriel Staples avatar
cn flag
@Rinzwind, that looks super useful. If you or I or someone can come up with some Windows + D keypress examples using some of those other tools, let's make that an answer here. It's not quite a duplicate.
Gabriel Staples avatar
cn flag
Using `ydotool`, I got it! It's complicated. I'll write about it tomorrow hopefully.
Gabriel Staples avatar
cn flag
I'll add an answer to my question here when I get a chance. It took me some time, but I just figured out a way with `ydotool`.
Gabriel Staples avatar
cn flag
Related (but not a duplicate) Q&A: [How can I install the latest ydotool 1.0.1 (keyboard automation tool working on Wayland) in Ubuntu 22.04?](https://askubuntu.com/q/1413829/327339)
Score:1
cn flag

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?
vanadium avatar
cn flag
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.
Gabriel Staples avatar
cn flag
@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`?
vanadium avatar
cn flag
It is outlined in the answer I posted about compiling ydotool: https://askubuntu.com/a/1413830/558158
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.