Linux的小折腾

Centos7 上安装使用xdotool,自动化脚本工具

2019-05-11  本文已影响1人  请叫我雯子小姐的小爷

在Centos7上如果需要使用自动化脚本,那xdotool肯定当仁不让了,号称Linux下的按键精灵,可以自动输入指令,移动鼠标,敲击键盘等等,下面就是安装使用xdotool的教程

  1. 安装xdotool,在bash中输入下面的两条指令即可安装
#/bin/bash
sudo yum install epel-release
sudo yum install xdotool
echo "Done"
  1. xdotool可使用的命令
  getactivewindow
  getwindowfocus
  getwindowname
  getwindowpid
  getwindowgeometry
  getdisplaygeometry
  search
  selectwindow
  help
  version
  behave
  behave_screen_edge
  click
  getmouselocation
  key
  keydown
  keyup
  mousedown
  mousemove
  mousemove_relative
  mouseup
  set_window
  type
  windowactivate
  windowfocus
  windowkill
  windowmap
  windowminimize
  windowmove
  windowraise
  windowreparent
  windowsize
  windowunmap
  set_num_desktops
  get_num_desktops
  set_desktop
  get_desktop
  set_desktop_for_window
  get_desktop_for_window
  get_desktop_viewport
  set_desktop_viewport
  exec
  sleep
  1. xdotool的基本使用
xdotool type 'hello' #模拟键盘输入hello
xdotool key Return #模拟键盘敲击回车键
xdotool key alt+Tab #模拟键盘敲击组合键(alt+tab)
xdotool keydown super #模拟键盘按下Windows键不放
xdotool keyup super #模拟键盘放开Windows键
xdotool mousemove 100 100 #模拟鼠标移动到(100, 100)位置
xdotool mousemove 100 100 click 1 #模拟鼠标移动到(100, 100)位置并点击左键 | click 2 是右键
  1. 搭配watch使用,可以实现循环输入(每10秒敲击一次回车键):
watch -n 10 xdotool key Return

也可以做到循环脚本test.sh,举个例子:

#!/bin/bash
xdotool mousemove 100 100 #将鼠标移动到(100, 100)
sleep 1 #停止1秒
xdotool mousemove 0 0 #将鼠标移动到(0, 0)

使用的时候,在terminal输入以下命令就能循环起来啦(每过一秒执行一次test.sh)

watch -n 1 sh test.sh
  1. 当然,如果也可以搭配Python来使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-

#第一种方法,使用call
from subprocess import call
call("xdotool key Return",shell=True)

#第二种方法,使用os
import os
os.system("xdotool key Return")
#如果想要得到输入命令后的反馈,可以使用os.popen
msm = os.popen('xdotool key Return').readlines()
msm = msm[0]

如有疑惑可以评论或者微信公众号留言

上一篇下一篇

猜你喜欢

热点阅读