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

发布于 2024-12-20 23:43:05 字数 53 浏览 0 评论 0 原文

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

I need to get the current mouse coordinates in bash and xdotool doesn't seem to be working for me. How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

荒岛晴空 2024-12-27 23:43:05

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

xdotool getmouselocation --shell

特别是,

eval $(xdotool getmouselocation --shell)

将位置放入 shell 变量 XYSCREEN 。之后,

echo $X $Y

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


我的额外的顺序点击几个位置是一个文件positions.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

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

xdotool getmouselocation --shell

In particular,

eval $(xdotool getmouselocation --shell)

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

echo $X $Y

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


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

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
孤独岁月 2024-12-27 23:43:05

尝试一下:

# 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 版本之间的工作方式可能不同。

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:

# 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 only mouse 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+c will do for ending the recording session.

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

(Edit)

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

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)

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

你另情深 2024-12-27 23:43:05

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

我假设(在窗户前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 位置行,那么通过管道将其传输到 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

What you meant by xdotool not working?

What's the output of

xdotool getmouselocation

Anyway, if you can compile a C program: 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:

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 STDERR like:

xdotool getmouselocation 2>/dev/null

That would skip the warning.

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

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

If you want to use the coordinates (with 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

并安 2024-12-27 23:43:05

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

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

If you're using xterm, you can issue an escape sequence ESC [ ? 9 h which 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.

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

行雁书 2024-12-27 23:43:05

我收到警告:XTEST 扩展在“(null)”上不可用。某些功能可能会被禁用;有关更多信息,请参阅“man xdotool”。 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"

很多方法来给这只猫剥皮。

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:

  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($0,a,":");} a[1]~/^[xy]$/{print a[2];}'
  // returns something like "2931 489 "

or

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

Plenty of ways to skin this cat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文