xdoctl 通过包装 xdotool 提供的各种操作和命令,提供一种使用 Python 操作 xdotool 工具的方式。
- 直接从仓库安装:
pip install git+https://github.com/nukemiko/python-xdoctl
- 或者到 发布页面 下载打包好的版本,然后使用
pip
安装
>>> import xdoctl
>>> xdoctl.mouse.get_location()
MouseLocation(x=481, y=1088, screen=0, window=123732042)
>>> xdoctl.mouse.move(1920, 1080)
>>> xdoctl.mouse.get_location()
MouseLocation(x=1920, y=1080, screen=0, window=123732042)
>>> xdoctl.mouse.click(1) # 鼠标左键
>>> xdoctl.mouse.click(3) # 鼠标右键
>>> xdoctl.mouse.click(2) # 鼠标中键
>>> xdoctl.keyboard.key_press('A') # 输入单个字符
>>> xdoctl.keyboard.key_press('E', 'x', 'a', 'm', 'p', 'l', 'e') # 输入一个单词
>>> xdoctl.keyboard.key_press(*list('Example')) # 等效于 `keyboard.key_press('E', 'x', 'a', 'm', 'p', 'l', 'e')`
>>> xdoctl.keyboard.key_press(*list('This'), 'space', *list('is'), 'space', 'a', 'n', *list('example')) # 输入一个句子
使用此方法输入长文本,比 keyboard.key_press() 更高效和易用
>>> xdoctl.keyboard.type_('This is an example')
>>> xdoctl.window.select_via_mouse()
123732042
>>> xdoctl.window.search('Konsole')
(138412036, 138412041, 138412039)
>>> xdoctl.window.search(38616, pattern_is_pid=True)
(138412039,)
>>> xdoctl.window.get_window_name(140509185) # 窗口标题
'Visual Studio Code'
>>> xdoctl.window.get_window_pid(140509185) # 窗口所属进程的 PID
43496
>>> xdoctl.window.get_window_geometry(140509185) # 窗口的几何属性(包括位置、大小、所在屏幕等)
WindowGeometryAttributes(x=312, y=504, width=1848, height=888, screen=0)
>>> xdoctl.window.window_move(140509185, 100, 100) # 移动窗口到 (100, 100);相对于坐标原点(位于屏幕左上角)移动
>>> xdoctl.window.get_window_geometry(140509185)
WindowGeometryAttributes(x=0, y=49, width=1848, height=888, screen=0)
>>> xdoctl.window.window_move(140509185, 800, 600, relative=True) # 移动窗口到 (900, 700);相对于原窗口位置 (100, 100) 移动
>>> xdoctl.window.get_window_geometry(140509185)
WindowGeometryAttributes(x=312, y=504, width=1848, height=888, screen=0)