我正在尝试编写一个BASH脚本,当程序的窗口有焦点时,它将执行以下操作。这将在KDE项下进行:
按住鼠标左键时:
单按滑鼠按钮4:
我有下面的代码,但看起来很慢。有优化的方法吗?
#!/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如能提供任何帮助,将不胜感激。
发布于 2022-10-19 07:55:15
几件事突然浮现在脑海里.
首先,如果鼠标左键没有按下,你似乎对像素的颜色不感兴趣,所以如果不是这样的话,我会避免得到所有的颜色。我是说:
while :; do
state="$(xinput --query-state "$mouse")"
# Don't do all the xwd | convert | grep stuff here
# if lmb (mouse 1) pressed
if [[ "$state" == *"button[1]=down"* ]]; then
# Do xwd | convert | grep stuff here
fi
done这样你就可以更频繁地测试了。
其次,调用xwd,它启动一个进程并捕获兆字节的数据,然后启动convert,这是另一个接收兆字节数据的进程,然后启动grep。你做了5次才能得到5个像素。所以,不要这样,启动一个xwd,一个convert,然后一次得到你的5个像素。
我不是使用xwd,而是在这里每次生成一个可重复的、固定的映像。您的代码是这样做的:
# Make same random image 5 times and extract a single pixel each time
magick -seed 42 -size 100x100 xc: +noise random -depth 8 -crop 1x1+10+10 txt:
magick -seed 42 -size 100x100 xc: +noise random -depth 8 -crop 1x1+20+20 txt:
magick -seed 42 -size 100x100 xc: +noise random -depth 8 -crop 1x1+30+30 txt:
magick -seed 42 -size 100x100 xc: +noise random -depth 8 -crop 1x1+40+40 txt:
magick -seed 42 -size 100x100 xc: +noise random -depth 8 -crop 1x1+50+50 txt:它产生了这样的结果:
# ImageMagick pixel enumeration: 1,1,0,255,srgb
0,0: (120,134,192) #7886C0 srgb(120,134,192)
# ImageMagick pixel enumeration: 1,1,0,255,srgb
0,0: (86,188,188) #56BCBC srgb(86,188,188)
# ImageMagick pixel enumeration: 1,1,0,255,srgb
0,0: (108,12,24) #6C0C18 srgb(108,12,24)
# ImageMagick pixel enumeration: 1,1,0,255,srgb
0,0: (174,137,144) #AE8990 srgb(174,137,144)
# ImageMagick pixel enumeration: 1,1,0,255,srgb
0,0: (44,185,31) #2CB91F srgb(44,185,31)我的建议是:
# Make same random image, but extract 5 pixels in one fell swoop
magick -seed 42 -size 100x100 xc: +noise random -depth 8 \
\( -clone 0 -crop 1x1+10+10 \) \
\( -clone 0 -crop 1x1+20+20 \) \
\( -clone 0 -crop 1x1+30+30 \) \
\( -clone 0 -crop 1x1+40+40 \) \
\( -clone 0 -crop 1x1+50+50 \) \
-delete 0 -append txt:它只在一个进程中在单个5像素图像中产生相同的5个像素,而不是在5个xwd进程和5个convert进程中产生相同的5个像素:
# ImageMagick pixel enumeration: 1,5,0,255,srgb
0,0: (120,134,192) #7886C0 srgb(120,134,192)
0,1: (86,188,188) #56BCBC srgb(86,188,188)
0,2: (108,12,24) #6C0C18 srgb(108,12,24)
0,3: (174,137,144) #AE8990 srgb(174,137,144)
0,4: (44,185,31) #2CB91F srgb(44,185,31)您可以考虑沿着以下几行将前一行的输出沿这些行输送到awk中:
magick -seed 42 -size 100x100 xc: +noise random -depth 8 \
\( -clone 0 -crop 1x1+10+10 \) \
...
\( -clone 0 -crop 1x1+50+50 \) \
-delete 0 -append txt: |
awk 'NR==1 {next} /#FFFFFF/ {print "white"; next} {print "not"}'然后你会得到一些这样的东西:
not
white
white
not
not这里是我最大的努力,请记住,我没有X11,甚至没有一台用于测试的计算机:
while : ; do
# Check if mouse pressed
state="$(xinput --query-state "$mouse")"
if [[ "$state" == *"button[1]=down"* ]]; then
read color1 color2 color3 color4 color5 < <( xwd -root -silent |
convert xwd:- -depth 8 \
\( -clone 0 -crop 1x1+195+247 \) \
\( -clone 0 -crop 1x1+1407+681 \) \
\( -clone 0 -crop 1x1+1200+256 \) \
\( -clone 0 -crop 1x1+1095+257 \) \
\( -clone 0 -crop 1x1+1195+258 \) \
-delete 0 -append txt: |
awk '
NR==1 { out =""; next }
/#FFFFFF/ { out = out "white "; next } { out = out "not " }
END { print out }
' )
echo $color1 $color2 $color3 $color4 $color5
fi
done如果它有一些bug,您可以这样运行来调试它:
bash -xv THISSCRIPT.sh或者粘贴到壳检查中。
https://stackoverflow.com/questions/74101045
复制相似问题