I want to type a character in a shell and have xdotool
send that character's signal to gedit, so that character will be typed in gedit.
I have written this script:
#!/bin/bash
gedit -s &
GEDPID=$!
sleep 2s
GEDWINID=`xdotool search --pid $GEDPID | tail -1`
echo "press any keys"
read i
xdotool windowactivate --sync $GEDWINID key --clearmodifiers --delay 100 "$i" && wmctrl -a Terminal
Everything works well except that the script waits for enter to send a keyboard signal to gedit.
So I've changed read i
to read -n1 i
, to make the script do its job without enter.
#!/bin/bash
gedit -s &
GEDPID=$!
sleep 2s
GEDWINID=`xdotool search --pid $GEDPID | tail -1`
echo "press any keys"
read -n1 i
xdotool windowactivate --sync $GEDWINID key --clearmodifiers --delay 100 "$i" && wmctrl -a Terminal
But it doesn't type any characters in gedit!
And here is the question, what is the second script's issue? What's difference between read i
and read -n1 i
that causes this problem?
I was able to recreate this issue. While I don't know why there's a difference between
read
andread -n1
, adding a simple delay before the key made it work. My guess is that there isn't enough time after the window switches for it to register the keypress.I modified your original script so it properly gets window ID of your current terminal and switches focus between the Terminal and Gedit windows. This script uses infinite loop, so all keystrokes being print in Terminal window are transferred into Gedit. Cancel it with Ctrl+C.