M BUZZ CRAZE NEWS
// news

Is it possible to send keystrokes to an inactive window?

By John Parsons

I would like to know if anyone knows a method where I can have a window focused, e.g a browser, and send keystrokes to an inactive window, e.g a text editor. For example, writing something whilst having a text editor minimised (this is not the specific task I'm doing, it's an example).

1

1 Answer

mzke a script that brings up a text box, then xdotool to activate the editor and paste the text, then reactivate your current window. set up ubuntu keyboard shortcut to run the script.

this one if for a yes/no.. modify for text input style

dialogue --man

has one for --inputbox which would be suitable for your application. you'll need to do that yourself.

sudo apt install dialog
dialog --title "texttoeditor" \
--backtitle "Suspend Confirmation " \
--yesno "Are you sure you want to suspend
\"/tmp/foo.txt\"?" 7 60
# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
0) systemctl suspend -i ;;
1) echo "suspend cancelled.";;
255) echo "[ESC] key pressed.";;
esac
text=<however we get the result as text for that input
dialogue for use below by xdotool
clear
exit

now you have the text entered, copy that to the clipboard or store as a variable $text for the script

sudo apt install xdotool

we can get the active window and store it's id, activate temporarily the texteditor (gedit used here), paste the text in from the clipboard using the editors keystrokes (eg ctrl-v ) , then reactivate the active window again. aggh, that wont work as the current window is the dialogue, so you would need to put the following id1 line BEFORE the dialogue above is called.

store the current windows id id1= $(xdotool getwindowfocus) # xdotool needs an id from the editor's name

name="gedit"

id2=$(xdotool search --desktop 0 --class "$name")

 xdotool windowactivate --sync $id2 key --clearmodifiers --delay 100
xdotool keydown ctrl && xdotool key v

better alternative is

xdotool type --window $id2 $text

more info on xdotool here xdotool windowactivate --sync $id1 key --clearmodifiers --delay 100

some work there for you, I have not tested all of those.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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