22

How can I configure a shortcut key to send a text string to the current program?

The purpose is to type common entries quicker (email address, street address, phone number, username, favorite quote, etc).

I don't need any further automation than just entering the text.

Gentoo Linux (3.2.12-gentoo) Xfce Desktop Environment (Version 4.8)

5 Answers 5

26

One simple approach is xdotool, like

xdotool type 'text'
7
  • That's half the solution... how do I assign a keyboard shortcut to run that? Making a shortcut in Xfce 4.8 Settings, Keyboard didn't work.
    – Steven
    Commented Apr 20, 2012 at 2:24
  • @Steven Does it work directly from a terminal?
    – jw013
    Commented Apr 20, 2012 at 2:31
  • 4
    Yes. Also, 'sleep 5 && xdotool type text' sends the text to the "active" window.
    – Steven
    Commented Apr 20, 2012 at 2:35
  • 2
    @Steven I haven't used Xfce in a while, but my next hunch is that when the command is run, the active window is wrong somehow. I'd try running xdotool getwindowfocus both from a terminal and as a keyboard shortcut while the same terminal is open and compare the results for equality. You'll probably have to redirect the keyboard shortcut version to a temporary file in order to see it.
    – jw013
    Commented Apr 20, 2012 at 2:40
  • 4
    I use xbindkeys.
    – Mikel
    Commented Apr 20, 2012 at 2:47
7

The xdotool works. However if you integrate it with the keyboard shortcut .. a simple xdotool type 'text' fails.

THe following shell script helped me in that

windowid=$(xdotool getwindowfocus)
sleep 0.5 && xdotool windowactivate --sync $windowid type 'text'
1
  • 2
    The manuals for xdotool show that it has an internal sleep. xdotool sleep 0.3 type 'Emotional 2003' suffices. (The short sleep is necessary for some reason, I don’t know. It seems like there is a short switch of the activated or focused window when running a command?)
    – k.stm
    Commented Nov 15, 2018 at 17:40
5

You may use AutoKey. Here is the description from the web site. “AutoKey is a desktop automation utility for Linux and X11. It allows you to manage collection of scripts and phrases, and assign abbreviations and hotkeys to these. This allows you to execute a script or insert text on demand in whatever program you are using.”

It is packaged, as far as I can tell, for Gentoo (announced here); for Ubuntu (here); Debian (here)...

2

One-liner that worked for me (stolen from the comment by k.tsm)

xdotool sleep 0.3 type 'string that you want to type / paste'

Then assign a shortcut to this command in KDE and after your pressing the shortcut, the required text is magically typed for you.

0

you can use this to control open file dialog input in linux :

import pyautogui
import subprocess , time

pyautogui.hotkey('ctrl', 'o' , interval = 0.15)
pyautogui.hotkey('ctrl', 'l' , interval = 0.15)
subprocess.Popen("xdotool sleep 0.3 type  '{Path_file}'".format(Path_file = "............................") , shell = True)
time.sleep(10)
pyautogui.press('enter')

You must log in to answer this question.

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