There are some packages that you can use such as xdotool
(discussed in this answer), ydotool
and wtype
. All three can be found in the Ubuntu 21.04 repos, but only xdotool is in the prior versions. xdotool uses X, but it will still work on Wayland, though it seems to be a little unpredictable. wtype works with Wayland, but at least currently (as of Ubuntu 21.04) it only produces the error: "Compositor does not support the virtual keyboard protocol". ydotool does not rely on X and works fine on both X and Wayland. It feels as if it is more speedy and consistent then xdotool.
The linked question details how to use xdotool, and ydotool is pretty similar--at least for basic tasks like just typing.
#!/bin/bash
ydotool type "1234"
You can save that snippet to a file in your path, e.g. $HOME/.local/bin, and mark it as executable to be called by your desktop enviroment's keyboard shortcut program.
# make executable
chmod +x /path/to/your/file.sh
On Gnome, search for 'Keyboard shortcuts' create a new custom one from the options at the bottom. For the command, enter the full path to the file containing the snippet, e.g. /home/yourusername/.local/bin/filename.sh
.
Another option is pyautogui, which is discussed in another answer from the linked one above, though instead of using it to press keys you can use its write()
function. Though, pip would be needed to install that package since its not in the Ubuntu repositories, and using a virtual environment to install it in is probably a good idea. But for a just outputting text, that could be overkill, especially if it performed no better than ydotool.
Example using pyautogui
#!/bin/bash
source /your_chose_path_here/venv/bin/activate
python3 -c 'import pyautogui; pyautogui.write("1234")'
Installing pyautogui
# install the virtual environment package to keep pip
# packages easily separate from the system
apt install python3-virtualenv
# create the virtual environment ($HOME/.local/venv is a nice place)
virtualenv --system-site-packages -p python3 /your_chose_path_here/venv
# activate the virtual environment, pointing it at the bin/activate
# file in the folder you just made in the previous step
source /your_chose_path_here/venv/bin/activate
# install pyautogui
pip install --upgrade pyautogui
# you can leave the virtual environment with the command:
deactivate