我如何在 bash 中获取当前的鼠标坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8480073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:20:11  来源:igfitidea点击:

How would I get the current mouse coordinates in bash?

bashshellunixmouse

提问by t3hcakeman

I need to get the current mouse coordinates in bash and xdotool does not work for me. How would I do this?

我需要在 bash 中获取当前鼠标坐标,而 xdotool 对我不起作用。我该怎么做?

回答by Lighty

To avoid all the sed/awk/cut stuff, you can use

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

xdotool getmouselocation --shell

In particular,

特别是,

eval $(xdotool getmouselocation --shell)

will put the position into shell variables X, Yand SCREEN. After that,

将位置放入 shell 变量X,YSCREEN. 在那之后,

echo $X $Y

will give a snippet ready for a later xdotool mousemoveor any other use.

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



My extra for sequential clicking into a few positions is a file positions.txt (given by a few eval/echo runs):

我用于连续点击几个位置的额外内容是一个文件positions.txt(由一些eval/echo运行给出):

123 13
423 243
232 989

And the code that uses it is:

使用它的代码是:

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

If there is no need to scale pixels (unlike my case), it could be a simple

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

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

回答by Yokai

Try this out:

试试这个:

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

This will show your mouse location at "x" and "y" in real time as you move it. You can save your coordinates into a file for later referencing or to use in a script to automate those mouse movements in the following way:

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

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

This^ will record onlymouse coordinates into coordinates.txt. You can use each line in a script if you want to repeat the actions taken while recording. A simple ctrl+cwill do for ending the recording session.

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

This is just a small sample of how awesome and practical xdotoolcan be for AFK automation and other things. Even custom bots :D

这只是xdotoolAFK 自动化和其他方面的出色和实用的一小部分示例。甚至自定义机器人:D

(Edit)

(编辑)

If you need to strip away the x:and y:from the sedcommand, you can add the logical OR |, while using the -Eoption for extended regex, operator as follows:

如果您需要从命令中删除x:and ,您可以添加逻辑 OR ,同时使用扩展正则表达式的选项,操作符如下:y:sed|-E

xdotool getmouselocation | sed -E "s/ screen:0 window:[^ ]*|x:|y://g"

xdotool getmouselocation | sed -E "s/ screen:0 window:[^ ]*|x:|y://g"

And if you want to use redirection and command substitution for a more compact command, you can use the following rather than a pipe:

如果您想对更紧凑的命令使用重定向和命令替换,您可以使用以下命令而不是管道:

sed -E 's/ screen:0 window:[^ ]*|x:|y://g' <<< $(xdotool getmouselocation)

sed -E 's/ screen:0 window:[^ ]*|x:|y://g' <<< $(xdotool getmouselocation)

As a disclaimer, the sed regex is written for GNU sed and may not work the same across different platforms or sed versions.

作为免责声明,sed 正则表达式是为 GNU sed 编写的,在不同的平台或 sed 版本中可能不会以相同的方式工作。

回答by Zsolt Botykai

What you meant by xdotoolnot working?

xdotool说不工作是什么意思?

What's the output of

输出是什么

xdotool getmouselocation

Anyway, if you can compile a Cprogram: http://dzen.geekmode.org/dwiki/doku.php?id=misc:xget-mouse-position

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

Regarding your comment below, you wrote you 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

I assume (in front of Windows XP) that you get it on two lines like:

我假设(在 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

If that's the case, you should redirect STDERRlike:

如果是这种情况,您应该STDERR像这样重定向:

xdotool getmouselocation 2>/dev/null

That would skip the warning.

那将跳过警告。

If your only input is the cursos positon line then piping that to sedwill give you the coordinates like this:

如果您唯一的输入是cursos 位置线,那么通过管道sed将其提供给您这样的坐标:

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

If you want to use the coordinates (with bash):

如果要使用坐标(带bash):

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

HTH

HTH

回答by evil otto

If you're using xterm, you can issue an escape sequence ESC [ ? 9 hwhich will make xterm send an escape sequence to the controlling program (i.e., bash) when you click with the mouse. I don't know if other terminal emulators have similar functionality.

如果您使用的是 xterm,您可以发出一个转义序列ESC [ ? 9 h,当您用鼠标单击时,该转义序列将使 xterm 向控制程序(即 bash)发送一个转义序列。不知道其他终端模拟器有没有类似的功能。

Info on mouse tracking in xterm is at http://www.xfree86.org/current/ctlseqs.html#MouseTracking

有关 xterm 中鼠标跟踪的信息位于http://www.xfree86.org/current/ctlseqs.html#MouseTracking

回答by ghoti

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

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

So it IS working for you. You just need to parse the ouput of the command. You can use the sed script zsolt posted above, or a variety of other options:

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

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

or

或者

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

or

或者

##代码##

Plenty of ways to skin this cat.

有很多方法可以给这只猫剥皮。