2

I have some input field of printable ASCII characters, excluding whitespace, that I'd like to pass to xdotool to type as if typed by keyboard.

My attempts:

  • echo "type lol lol" | xdotool - eats the space in the middle, so it types lollol instead of lol lol.
  • echo "type 'lol lol'" | xdotool - correctly types lol lol, but it cannot type ' in the middle. I.e. echo "type 'lol ' lol'" | xdotool - doesn't type lol ' lol, but rather types lol lol'.

My question:

  • How to use xdotool to let it type arbitrary characters without the problem above?
  • In case xdotool is not adequate, got any alternative tool that work better?

Update: reason I care about this is because I'm using it for a password manager that I'm implementing. I think it's better to type passwords by means of keyboard typing emulation, as opposed to copying-pasting via some shared clipboard, which other processes may look into to steal passwords.

4

I do not understand why you have to echo the command via stdin, you can use the command directly. The following works for me (tested using xev):

$ xdotool search Event
Defaulting to search window name, class, and classname
37748737
$ xdotool type --window 37748737 "'"
$ xdotool type --window 37748737 " "

If you insist on passing the command via stdin, then apparently xdotool does its own parsing, so you have to escape double quotes:

$ echo "type --window 37748737 \"' \"" | xdotool -

For finer control, you can also use key with keysyms separated by space:

$ xdotool key --window 37748737 space a apostrophe

This is usually enough for any kind of automation you want. Note that xdotool simulates key presses and releases, not characters.

You have not described your use case (please edit your question and do so, and also read about the XY problem), but if for some reason you want paste variable text from some other source into some application, consider using the clipboard (via xclip), and simulating a paste action with xdotool in the input field - this will be much more reliable than trying to translate arbitrary text into appropriate key presses.

3
  • I'm using this for a password manager. Not storing passwords in clipboards seems better security wise. Passwords could be generated automatically, containing various symbols, hence the requirement of passing arbitrary characters to xdotool.
    – caveman
    Jun 4 at 3:51
  • Note that the X "clipboard" isn't really a clipboard, it's a communication protocol between applications. If your password manager is an X application, make it take the primary selection, then the password will never be "stored in a clipboard" and your password manager can verify that the paste request is coming from the right application. And while you are at that, you can also implement what xdotool does to trigger the paste. This will need some old-fashioned C programming skills, and knowledge of the X libraries.
    – dirkt
    Jun 4 at 5:42
  • That said, trying to move arbitrary symbols (including UTF-8 ...) to a password input field by simulating key presses is going to fail for more complicated symbols - you have no idea in general what keys the underlying UI of the password manager will need for some symbols. So the X selection ("clipboard") protocol is the way to go. (Please also read up on XY questions, you should have mentioned "I want to write a password manager" as the first thing in your question).
    – dirkt
    Jun 4 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.