ydotool icon indicating copy to clipboard operation
ydotool copied to clipboard

Tutorial: how to install and use `ydotool`

Open ElectricRCAircraftGuy opened this issue 1 year ago • 15 comments

You might consider linking to this in your documentation. This was a lot of work for me to figure it out, so I think it will be useful to many people:

Tutorial: Getting started with ydotool to automate key presses (or mouse movements) in Linux

I go over installation, launching the daemon in a way that doesn't require your user to use root to connect to it, pressing key sequences, and where to find them, how to view the help menus, etc.

ElectricRCAircraftGuy avatar Jun 18 '23 03:06 ElectricRCAircraftGuy

Just in time, I was getting lost trying to install it.

Still having trouble finding how to simulate arrow keys.

Oh, it's just KEY_RIGHT

rwjack avatar Jun 19 '23 12:06 rwjack

@rwjack , yeah, it appears to be KEY_RIGHT, or key code 106, as shown here: https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h#L181

You can view all key codes locally with:

gedit /usr/include/linux/input-event-codes.h

For full instructions to press and release KEY_RIGHT, it should be this:

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

# start the background daemon
sudo -b ydotoold --socket-path="$HOME/.ydotool_socket" --socket-own="$(id -u):$(id -g)"

# press key right, then release it
YDOTOOL_SOCKET="$HOME/.ydotool_socket" ydotool key 106:1 106:0

I don't mention it in my article, but you can also add that environment variable to the bottom of your ~/.bashrc file, as this:

export YDOTOOL_SOCKET="$HOME/.ydotool_socket"

Then you could just run this in place of the last command above:

ydotool key 106:1 106:0

ElectricRCAircraftGuy avatar Jun 19 '23 16:06 ElectricRCAircraftGuy

@ElectricRCAircraftGuy Kudos! Thanks bud

rwjack avatar Jun 19 '23 16:06 rwjack

@ElectricRCAircraftGuy That was a great guide, thank you. I made a script (that i have named ydotool-manage on my computer) based on your guide to start, stop and update from one place.

#!/bin/sh

if [ $# -ne 1 ] || ( [ "$1" != "start" ] && [ "$1" != "stop" ] && [ "$1" != "update" ] && [ "$1" != "status" ] );then
    echo "`basename $0`: missing OPTION
Usage: `basename $0` [OPTION]

    '`basename $0` start' to start the ydotool service.
    '`basename $0` stop' to stop the ydotool service
    '`basename $0` update' to update (or install) ydotool."
  exit 1
fi

# Visa status
if [ "$1" = "status" ]; then
    sudo systemctl status ydotool.service
fi

# Start ydtoold
if [ "$1" = "start" ]; then
    sudo systemctl start ydotool.service
    sudo systemctl status ydotool.service
fi

# Stop ydtoold
if [ "$1" = "stop" ]; then
    sudo systemctl stop ydotool.service
    sudo systemctl status ydotool.service
fi

# Update (update git folder, build and make install) ydotool and ydotoold.
if [ "$1" = "update" ] ; then
    sudo -v

    # pre reqs
    sudo apt update && sudo apt install cmake scdoc pkg-config

    # Check if ydotool repo is downloaded
    if [ ! -d /opt/ydotool ]; then 
        cd /tmp
        git clone https://github.com/ReimuNotMoe/ydotool.git
        sudo mv /tmp/ydotool/ /opt/
    fi

    cd /opt/ydotool/

    # Get latest version
    git pull

    # Make
    mkdir -p build && cd build
    cmake ..
    time make -j "$(nproc)"

    # Install
    sudo make install

    # Remove the need for sudo
    sudo chmod +s $(which ydotool)

    # Install systemd service
    sudo rm /etc/systemd/system/ydotool.service
    sudo ln -s /usr/lib/systemd/user/ydotool.service /etc/systemd/system/
    # Reload the systemd daemon
    sudo systemctl daemon-reload
    # Reload enable the service
    sudo systemctl enable ydotool
    # Reload start ydotool
    sudo systemctl start ydotool
    # Check that it is running
    sudo systemctl status ydotool

    echo "ydotoold version: $(ydotoold --version)"
    echo ""

    if ! grep -q 'export YDOTOOL_SOCKET="/tmp/.ydotool_socket"' $HOME/.profile; then
        echo '' >> "$HOME/.profile"
        echo '# ydotoold socket location' >> "$HOME/.profile"
        echo 'export YDOTOOL_SOCKET="/tmp/.ydotool_socket"' >> "$HOME/.profile"
        echo "YDOTOOL_SOCKET is being exported in $HOME/.profile."
        echo "Please relog to enable the new profile settings."
    fi

fi

EDIT: Now properly installs the service. More information here https://askubuntu.com/questions/1413829/how-can-i-install-and-use-the-latest-ydotool-keyboard-automation-tool-working-o

EDIT 2: Enable the service after installing it.

EDIT 3: Forgot to install cmake scdoc pkg-config

ecspresso avatar Sep 18 '23 19:09 ecspresso

@ecspresso , nice job. There are still more things I don't know. Using systemd to stop/start/restart it may be better. Check out this comment here (all the comments under my answer): https://askubuntu.com/questions/1470593/how-can-i-write-a-program-to-press-keys-such-as-windows-d-in-wayland-repla#comment2596097_1473061

ElectricRCAircraftGuy avatar Sep 18 '23 19:09 ElectricRCAircraftGuy

I can see people talking about the service but it seems to be missing for me.

ecspresso avatar Sep 18 '23 19:09 ecspresso

I found this where someone explains how to build and install ydotool in detail. It is also explained how to add the service.

I have updated my above script.

ecspresso avatar Sep 18 '23 20:09 ecspresso

@ElectricRCAircraftGuy if you have time, let's update that guide, split them into how to install and a use guide? I can help with that. Then we can move towards submitting a PR mentioning those links as a guide

Paiusco avatar Mar 06 '24 19:03 Paiusco

I had it working in a previous system but didn't write it down so wasted quite a few hours figuring it out again, so I did write up here.

shellheim avatar Apr 25 '24 12:04 shellheim

@shellheim I think there are some improvements. I'm wondering that even the ydotool.service is now named ydotoold.service

You also don't need to chown on the socket created, ydotoold accepts this as a parameter when run, so you can edit the service file so you're the owner of the socket, or even change the permission (similar to what a chmod would do), so even that you're not the owner, you can still access the socket

Paiusco avatar Apr 28 '24 16:04 Paiusco

@Paiusco Man, I am so damm grateful to you. Thank you very much. I had completely forgotten about that flag. I am gonna fix the gist in the morning. Again, thank you very much!

edit: the gist should be much better by now.

shellheim avatar Apr 28 '24 17:04 shellheim

Thanks so much for your contribution, I'm very grateful.

I have pinned this issue now.

ReimuNotMoe avatar Apr 28 '24 21:04 ReimuNotMoe

@ElectricRCAircraftGuy if you have time, let's update that guide, split them into how to install and a use guide? I can help with that. Then we can move towards submitting a PR mentioning those links as a guide

I'm not going to have time :(

I'm a single dad now, and sole income.

ElectricRCAircraftGuy avatar Apr 30 '24 18:04 ElectricRCAircraftGuy

Is it not possible to make installation as compact as apt install ydotool?

Edwardius avatar May 20 '24 18:05 Edwardius

@Edwardius if you look at #233 you'll see that the package that debian-like distro currently ships is 5years old, so this is controlled by the distro/repo and not by us. If you want to get something newer, you may move to a "rolling distro" instead.

Paiusco avatar May 21 '24 07:05 Paiusco