[Linux]通过shell脚本控制鼠标画一个圈圈

原理:基于xdotool命令

xdotool命令可以让你获取鼠标在屏幕上的位置,并且让你能够控制你的鼠标移动!!!

xdotool命令安装:
sudo apt install xdotool

画圆算法:分为四个象限,对每个象限进行不同的处理

shell脚本代码如下:

1、以当前鼠标的停放位置为起点。
2、目前默认从圆的最左边开始画。
3、通过修改下方RADIUS变量的值来修改圆的半径。

#!/bin/bash

RADIUS=200

cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
circle_x=`echo "$cur_x+$RADIUS" | bc -l`
circle_y=$cur_y


echo "1quadrant"

for ((i = 0; i <= 90; i += 5)); do

  tmp=`echo "scale=10;$i*3.1415926/180" | bc`
  new_x=`echo "scale=10;$RADIUS*c($tmp)+0.5" | bc -l`
  new_y=`echo "scale=10;$RADIUS*s($tmp)+0.5" | bc -l`
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  if [[ $new_x -lt 1 ]]
  then
    new_x=0
  fi
  if [[ $new_y -lt 1 ]]
  then
    new_y=0
  fi
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  new_x=`echo "$circle_x-$new_x" | bc -l`
  new_y=`echo "$circle_y-$new_y" | bc -l`
  new_x_offset=`echo "$new_x-$cur_x" | bc -l`
  new_y_offset=`echo "$new_y-$cur_y" | bc -l`
  xdotool mousemove_relative -- $new_x_offset $new_y_offset

  sleep 0.001

  cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
  cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
done


echo "2quadrant"

cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
circle_x=$cur_x
circle_y=`echo "$cur_y+$RADIUS" | bc -l`
for ((i = 90; i <=180; i += 5)); do

  tmp=`echo "scale=10;$i*3.1415926/180" | bc`
  new_x=`echo "scale=10;$RADIUS*c($tmp)+0.5" | bc -l`
  new_y=`echo "scale=10;$RADIUS*s($tmp)+0.5" | bc -l`
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  if [ -z $new_x ];
  then
    if [[ $new_x -lt 1 ]]
    then
      new_x=0
    fi
  fi
  if [[ $new_y -lt 1 ]]
  then
    new_y=0
  fi
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  new_x=`echo $(($new_x*-1))`
  new_x=`echo "$circle_x+$new_x" | bc -l`
  new_y=`echo "$circle_y-$new_y" | bc -l`
  new_x_offset=`echo "$new_x-$cur_x" | bc -l`
  new_y_offset=`echo "$new_y-$cur_y" | bc -l`
  xdotool mousemove_relative -- $new_x_offset $new_y_offset

  sleep 0.001
  cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
  cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
done


echo "3quadrant"

cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
circle_x=`echo "$cur_x-$RADIUS" | bc -l`
circle_y=$cur_y
for ((i = 180; i <=270; i += 5)); do

  tmp=`echo "scale=10;$i*3.1415926/180" | bc`
  new_x=`echo "scale=10;$RADIUS*c($tmp)+0.5" | bc -l`
  new_y=`echo "scale=10;$RADIUS*s($tmp)+0.5" | bc -l`
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  if [ -z $new_x ];
  then
    if [[ $new_x -lt 1 ]]
    then
      new_x=0
    fi
  fi
  if [ -z $new_y ];
  then
    if [[ $new_y -lt 1 ]]
    then
      new_y=0
    fi
  fi
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  new_x=`echo $(($new_x*-1))`
  new_y=`echo $(($new_y*-1))`
  new_x=`echo "$circle_x+$new_x" | bc -l`
  new_y=`echo "$circle_y+$new_y" | bc -l`
  new_x_offset=`echo "$new_x-$cur_x" | bc -l`
  new_y_offset=`echo "$new_y-$cur_y" | bc -l`
  xdotool mousemove_relative -- $new_x_offset $new_y_offset

  sleep 0.001
  cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
  cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
done



echo "4quadrant"

cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
circle_x=$cur_x
circle_y=`echo "$cur_y-$RADIUS" | bc -l`
for ((i = 270; i <=360; i += 5)); do

  tmp=`echo "scale=10;$i*3.1415926/180" | bc`
  new_x=`echo "scale=10;$RADIUS*c($tmp)+0.5" | bc -l`
  new_y=`echo "scale=10;$RADIUS*s($tmp)+0.5" | bc -l`
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  if [ -z $new_x ];
  then
    if [[ $new_x -lt 1 ]]
    then
      new_x=0
    fi
  fi
  if [ -z $new_y ];
  then
    if [[ $new_y -lt 1 ]]
    then
      new_y=0
    fi
  fi
  new_x=${new_x%%.*}
  new_y=${new_y%%.*}
  new_y=`echo $(($new_y*-1))`
  new_x=`echo "$circle_x-$new_x" | bc -l`
  new_y=`echo "$circle_y+$new_y" | bc -l`
  new_x_offset=`echo "$new_x-$cur_x" | bc -l`
  new_y_offset=`echo "$new_y-$cur_y" | bc -l`
  xdotool mousemove_relative -- $new_x_offset $new_y_offset

  sleep 0.001
  cur_x=`xdotool getmouselocation | cut -d ' ' -f 1 | cut -d ':' -f 2`
  cur_y=`xdotool getmouselocation | cut -d ' ' -f 2 | cut -d ':' -f 2`
done

echo "draw finish!"

运行:

给予脚本可执行权限,直接运行即可!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值