Score:1

How do you set a keyboard shortcut to add specified text?

bd flag

i would like to set a shortcut so that when i type super + p, a set of numbers i use frequently are written wherever i'm writing, could someone help me with that?

(for example i'm using the search bar or the terminal, i type super+p and the numbers "1234" are written as if i just typed them)

ar flag
Which version of Ubuntu are you using?
Score:0
cn flag

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
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.