0

I created a script to type a random string of numbers by binding it to a hotkey:

#!/bin/bash

ran="$(shuf -i 100000-999999 -n 1)"

sleep .2

xdotool type --delay 12 "untitled_$ran "

For some reason, the xdotool command will not do anything for the first tenth of a second or so. If there is no sleep, nothing is typed. If the sleep command is shorter, fewer than six characters are typed. It's as if the command runs, but is ignored for the first few moments after the script is executed.

Why does this happen, and how can I resolve this?

  • There is no intrinsic reason why this should happen. How are you launching the script? Is there perhaps a keyboard focus change right after you do? Or are you running the script from a shell in the same terminal that imminently expects to receive the keypresses? By the way, your script is Bourne-compatible, so you could be using #!/bin/sh for greater portability. – Celada Aug 6 '17 at 17:08
  • @HaukeLaging How exactly is shuf an "extremely bad way"? shuf uses only a small amount of entropy for initialization and is pseudorandom. It's much better than $RANDOM, which doesn't even get seeded randomly and uses the current date instead. If you're concerned about code elegance, shuf is also more fitting since it doesn't require all kinds of arithmetic to get into a proper value range. – PawkyPenguin Aug 6 '17 at 18:08
  • @PawkyPenguin I misunderstood the way shuf works. I will delete the comment. – Hauke Laging Aug 6 '17 at 18:26
  • @HaukeLaging No worries. It's actually a good point that the source of randomness matters. In shuf's case, I'm fairly sure that the PRG only gets initialized at startup (or maybe the first time shuf is executed), because if I execute it multiple times in a row, my available entropy still doesn't go down. – PawkyPenguin Aug 6 '17 at 19:13
  • @Celada I'm firing the script from a hotkey, using the keyboard utility present in Linux Mint Cinnamon. – ArdentCertes Aug 7 '17 at 2:44
2

You've said in a comment that you are using some kind of hotkey to trigger this script. I can't be sure what's happening, but I suspect that the keyboard focus is being moved around and that xdotool, when run without delay, is being run while the focus is something other than the intended terminal. I recommend the following method for testing this hypothesis:

  • Start xev from a terminal.
  • Focus the xev window (move the mouse into it, or such) while keeping the terminal visible.
  • Press your hotkey.
  • Watch for FocusOut and FocusIn events recorded by xev. If any of these happen, they could explain why xev is missing keypresses.
  • KeyPress and KeyRelease events should show you xdotool doing its work.

If the hypothesis is correct, then as for how to resolve it, you'd have to activate the hotkey in some way that doesn't cause a focus change. I don't know...

  • Yep, I do see a change in focus when hitting my hotkey. This seems to explain the issue, and lines up with what I said about needing a delay at the start of the script. – ArdentCertes Aug 8 '17 at 4:01

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.