我将如何获得在bash当前的鼠标坐标?

我需要在bash中获取当前的鼠标坐标,xdotool不适用于我。 我将如何做到这一点?

为了避免所有的sed / awk / cut东西,你可以使用

 xdotool getmouselocation --shell 

尤其是,

 eval $(xdotool getmouselocation --shell) 

将该位置置于shell变量XYSCREEN 。 之后,

 echo $X $Y 

将为稍后的xdotool mousemove或任何其他用途提供一个片段。


我额外的顺序点击几个位置是一个文件positions.txt(由几个评估/回声运行):

 123 13 423 243 232 989 

而使用它的代码是:

 while read line; do X=`echo $line| cut -c1-3`; Y=`echo $line| cut -c4-7`; xdotool mousemove --sync $(( 0.5 + $X )) $(( 0.5 + $Y )); xdotool click 1 done < positions.txt 

如果不需要缩放像素(不像我的情况),它可能是一个简单的

 while read line; do xdotool mousemove --sync $line; xdotool click 1 done < positions.txt 

试试这个:

 # Real time mouse position. watch -t -n 0.0001 xdotool getmouselocation 

这会在您移动鼠标时实时显示“x”和“y”的鼠标位置。 您可以将坐标保存到文件中供以后参考,或在脚本中使用以下列方式自动执行鼠标移动:

 # Save real time mouse coordinates to file. while true; do xdotool getmouselocation | sed -e 's/ screen:0 window:[^ ]*//g' >> coordinates.txt; done 

这^ 将鼠标坐标记录到coordinates.txt中。 如果您想重复录制时采取的操作,则可以在脚本中使用每一行。 一个简单的ctrl+c将结束记录会话。

这只是一个小例子,它是如何真棒和实用的xdotool可以用于AFK自动化和其他的事情。 即使定制机器人:D

I get Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665

所以这是为你工作。 你只需要解析命令的输出。 您可以使用上面发布的sed脚本zsolt或其他各种选项:

  xdotool getmouselocation 2>/dev/null | cut -d\ -f1,2 - // returns something like "x:2931 y:489" 

要么

  xdotool getmouselocation 2>/dev/null \ | awk 'BEGIN{RS=" ";ORS=RS} {split($0,a,":");} a[1]~/^[xy]$/{print a[2];}' // returns something like "2931 489 " 

要么

  xdotool getmouselocation 2>/dev/null | sed 's/ sc.*//; s/.://g; s/ /x/' // returns something like "2931x489" 

很多方法来剥皮这只猫。

你的意思是由xdotool不工作?

什么是输出

 xdotool getmouselocation 

无论如何,如果你可以编译一个C程序: http : //dzen.geekmode.org/dwiki/doku.php?id=misc : xget-mouse-position

关于你下面的评论,你写的是:

 Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665 

我假设(在Windows XP的前面),你得到了两行,如:

 Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665 

如果是这样的话,你应该像下面这样重定向STDERR

 xdotool getmouselocation 2>/dev/null 

那会跳过警告。

如果你唯一的输入是cursos positon行然后管道到sed会给你这样的坐标:

 xdotool getmouselocation 2>/dev/null | \ sed 's/x:\([0-9]\+\)[ \t]y:\([0-9]\+\)[ \t].*/\1;\2/' # OUTPUT should by something like: "654;453" 

如果你想使用坐标(用bash ):

 export COORDINS=`xdotool getmouselocation 2>/dev/null | sed 's/x:\([0-9]\+\)[ \t]y:\([0-9]\+\)[ \t].*/\1;\2/'` export XPOS=${COORDINS/;*/} export YPOS=${COORDINS/*;/} 

HTH

如果你使用xterm,你可以发出一个转义序列ESC [ ? 9 h ESC [ ? 9 h ,当你用鼠标点击的时候,会使xterm向控制程序(即bash)发送一个转义序列。 我不知道其他终端仿真器是否具有类似的功能。

有关鼠标在xterm中的信息, 请访问http://www.xfree86.org/current/ctlseqs.html#Mouse跟&#x8E2A;