I am trying to code a BASH script that will do the following while a program's window has focus. This would be done under KDE:
While holding left mouse button:
- press alt-4 key
- check for color (white) at pixel position 1; if pixel color exists, press alt-1 key
- check for color (white) at pixel position 2; if pixel color exists, press alt-2 key
- check for color (white) at pixel position 3; if pixel color exists, press alt-3 key
Single press mouse button 4:
- toggle script sequence on/off
- press alt-5
I have the following code but it seems to be quite slow. Any way to optimize?
#!/bin/bash
# mouse id. use xinput --list. verify with xinput --query-state [id]
mouse=12
# set colors with xwd. run the following in terminal to get cursor position:
# while true; do xdotool getmouselocation; sleep 0.2; clear; done
while :; do
state="$(xinput --query-state "$mouse")"
color1="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+195+247" txt:- | grep -om1 '#\w\+')"
color2="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1407+681" txt:- | grep -om1 '#\w\+')"
color3="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1200+256" txt:- | grep -om1 '#\w\+')"
color4="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1095+257" txt:- | grep -om1 '#\w\+')"
color5="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1195+258" txt:- | grep -om1 '#\w\+')"
# if lmb (mouse 1) pressed
if [[ "$state" == *"button[1]=down"* ]]; then
if [[ "$color1" == "#FFFFFF" ]]; then
xdotool key --clearmodifiers a
elif [[ "$color2" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers b
elif [[ "$color3" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers c
elif [[ "$color4" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers d
elif [[ "$color5" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers e
fi
fi
done
Any help would be appreciated