【发布时间】:2011-12-12 19:56:27
【问题描述】:
我需要在 bash 中获取当前鼠标坐标,而 xdotool 似乎对我不起作用。我该怎么做?
【问题讨论】:
-
从哪里来? X服务器?诅咒? ...
-
xdotool getmouselocation报告什么错误?调试可能比提出替代方案更容易。
我需要在 bash 中获取当前鼠标坐标,而 xdotool 似乎对我不起作用。我该怎么做?
【问题讨论】:
xdotool getmouselocation 报告什么错误?调试可能比提出替代方案更容易。
要避免所有 sed/awk/cut 的东西,你可以使用
xdotool getmouselocation --shell
特别是,
eval $(xdotool getmouselocation --shell)
会将位置放入 shell 变量 X、Y 和 SCREEN。之后,
echo $X $Y
将为以后的xdotool mousemove 或任何其他用途提供一个 sn-p。
我对连续点击几个位置的额外操作是文件 position.txt(由几次 eval/echo 运行给出):
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
此^ 将仅 将鼠标坐标记录到坐标.txt 中。如果要重复录制时执行的操作,可以使用脚本中的每一行。一个简单的ctrl+c 即可结束录制会话。
这只是xdotool 对 AFK 自动化和其他事情的出色和实用性的一小部分示例。甚至自定义机器人:D
(编辑)
如果您需要从sed 命令中去除x: 和y:,您可以添加逻辑或|,同时使用-E 选项进行扩展正则表达式,运算符如下:
xdotool getmouselocation | sed -E "s/ screen:0 window:[^ ]*|x:|y://g"
如果您想使用重定向和命令替换来获得更紧凑的命令,您可以使用以下命令而不是管道:
sed -E 's/ screen:0 window:[^ ]*|x:|y://g' <<< $(xdotool getmouselocation)
作为免责声明,sed 正则表达式是为 GNU sed 编写的,可能在不同平台或 sed 版本中的工作方式不同。
【讨论】:
x: 和y: 吗?您将如何阅读它们并重播?
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
这将跳过警告。
如果您唯一的输入是光标位置线,那么通过管道将其传输到 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
【讨论】:
$ xdotool getmouselocation 2>/dev/nullx:720 y:439 screen:0 window:1665
如果您使用的是 xterm,您可以发出转义序列ESC [ ? 9 h,当您单击鼠标时,这将使 xterm 向控制程序(即 bash)发送一个转义序列。我不知道其他终端模拟器是否有类似的功能。
xterm 中鼠标跟踪的信息位于http://www.xfree86.org/current/ctlseqs.html#MouseTracking
【讨论】:
ESC [ ? 9 l 应该让xterm 退出这种模式会很方便,这样你的鼠标就会再次以它应该的方式工作。 :)
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"
给这只猫剥皮的方法很多。
【讨论】: