0

0

AutoKey - 适用于Linux和X11的桌面自动化应用程序

雪夜

雪夜

发布时间:2025-06-19 17:00:39

|

318人浏览过

|

来源于php中文网

原创

autokey - 适用于linux和x11的桌面自动化应用程序

1、前言

在当今软件开发领域,质量保证是项目成功的关键因素之一。随着软件迭代速度不断加快,传统手动测试方法难以满足与日俱增的测试需求,因此,自动化测试工具的重要性愈发突出。

本篇将介绍一个自动化图形界面应用 - AutoKey,能够运行Python3脚本并实现文本扩展,特别注重宏和按键功能。

2、简介

AutoKey是一个具有GTK和Qt版本的Python3自动化应用程序。它可以进行文本扩展、运行宏和运行脚本,其中任何一个都可以用热键、组合键或键入的缩写来触发,它是完全可定制的,你可以使用它来自动化几乎任何你可以想到的计算机行为。

AutoKey可用于简单的文本扩展(即用静态文本替换缩写词)。在AutoKey中,这些替换文本被称为“短语”。AutoKey还可以响应键盘快捷键(例如[Ctrl]+[Alt]+F8)来扩展短语。为了提高灵活性,你可以在短语中使用宏来动态更改输入的内容。

如果简单的短语扩展不能满足你的需求,您可以发挥Python编程语言的全部功力,用Python 3编写脚本来自动化你的任务。AutoKey脚本可以像短语一样绑定到缩写和快捷键上,并执行你的命令。AutoKey提供了一个API与系统交互,可实现诸如鼠标点击或使用键盘输入文本等操作。

安装:

pip3 install autokey

更多安装可参考:

https://github.com/autokey/autokey/wiki/Installing

Github地址:

https://github.com/autokey/autokey

3、快速上手

1、将文本转换为小写,并用连字符替换空格。

如果你遵循“命名分支”Git开发的风格,这对于将GitHub Issue的名称转换为适合Git分支的字符串非常有用。

例如:FocusProxy返回为:-focusproxy

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">text = clipboard.get_selection()clipboard.fill_clipboard(text.lower().replace(' ', '-'))</code>

2、获取当前平台信息。

可用于制作错误报告,特别是在平台和浏览器版本可能相关的web应用程序上。

例如:

Platform: Linux-4.13.0-37-generic-x86_64-with-LinuxMint-18.3-sylviaBrowser: Google Chrome 65.0.3325.181Browser: Mozilla Firefox 59.0.1

Date Tested :Wed 28 Mar 14:46:48 BST 2024

MusicAI
MusicAI

AI音乐生成工具

下载
代码语言:javascript代码运行次数:0运行复制
<code class="javascript">import platformoutput = ""output += "Platform: " + platform.platform() + "\n"output += "Browser: " + os.popen("google-chrome --version").read()output += "Browser: " + os.popen("firefox --version").read()output += "Date Tested :" + system.exec_command("date")keyboard.send_keys(output)</code>

3、按以下格式插入当前日期时间(YYYY.MM.DD HH:MM:SS)。

例如:2024.10.13 23:47:00

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%Y.%m.%d %H:%M:%S'))</code>

4、按以下格式插入当前时间(HH:MM:SS)。

例如:23:47:00

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%H:%M:%S'))</code>

5、按以下格式插入当前日期(YYYY.MM.DD)。

例如:2024.10.13

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%Y.%m.%d'))</code>

6、在浏览器中从剪贴板搜索文本。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">import webbrowserimport timetime.sleep(0.1)webbrowser.open("http://www.google.de/search?q="+clipboard.get_clipboard())</code>

7、打开某个网站。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">import webbrowserimport timetime.sleep(0.1)site = "baidu.com"webbrowser.get('google-chrome').open_new_tab(site)# webbrowser.get('firefox').open_new(site) # in case you want to open a new site in firefox# webbrowser.get('firefox').open_new_tab(site)</code>

8、获取当前鼠标坐标。

在弹出对话框中显示当前的X和Y鼠标坐标。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">from Xlib import X, display # import the necessary classes from the specified moduled = display.Display().screen().root.query_pointer() # get pointer locationx = str(d.root_x) # get x coord and convert to stringy = str(d.root_y) # get y coord and convert to stringdialog.info_dialog("(X, Y)", x+", "+y) # create an info dialog to display the coordinates</code>

9、使用键入或键入并单击输入的GUI对话框。

一种GUI对话框,使用键入或键入和单击输入的组合来启动程序或显示对话框。你可以自定义脚本以执行几乎任何你喜欢的操作。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">################################################################ ABOUT THIS SCRIPT################################################################ A GUI information dialog will be displayed asking for input.# This script will act on your input.################################################################ USE THIS SCRIPT################################################################ Set a Hotkey for this script (example: Ctrl+K).# The script will activate when you press Ctrl and K.# When it prompts you for input, you can choose ONE of these:    # Press the Esc key to cancel the dialog.    # ... or ...    # Click the Cancel button to cancel the dialog.    # ... or ...    # Press the Enter key to accept the default example text.    # ... or ...    # Click the OK button to accept the default example text.    # ... or ...    # Type in some text and press the Enter key.    # ... or ...    # Type in some text and click the OK button.################################################################ NOTES################################################################ When using subprocess, remember to use a comma wherever there is a space in a command.# The subprocess.call function is part of the older high-level API and has been replaced with the subprocess.run function.# The subprocess.call function waits for the process to complete and provides a return code with its exit status before allowing you to execute other code.# The subprocess.run function waits for the process to complete and provides a return code with its exit status before allowing you to execute other code.# The subprocess.Popen function allows you to execute other code and/or interact with the process with the subprocess.communicate function while the process is running.# Examples of some browsers that could have been used in the script:    # subprocess.Popen(["C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"])    # subprocess.Popen(["chrome.exe"])    # subprocess.Popen(["chromium-browser"])    # subprocess.Popen(["google-chrome"])    # subprocess.Popen(["google-chrome-stable"])################################################################ THE SCRIPT################################################################ Message to display to prompt the user for input:prompt="Please type one of these commands:\n\t* chro\n\t* fire\n\t* goo\n\t* viv\n\t* apple\n\t* banana\n\t* coconut"# Ask for input, offer a default example, and check the exit code of the command:retCode, userInput = dialog.input_dialog(title="Input required", message=prompt, default="example")# If the command was successful (the exit code was zero), take the user's desired action:if retCode == 0:    # If chro was typed, open Chromium:    if userInput == "chro":        program="chromium-browser"        subprocess.Popen([program])    # If fire was typed, open Firefox:    elif userInput == "fire":        program="firefox"        subprocess.Popen([program])    # If goo was typed, open Google Chrome:    elif userInput == "goo":        program="google-chrome"        subprocess.Popen([program])    # If viv was typed, open Vivaldi:    elif userInput == "viv":        program="vivaldi"        subprocess.Popen([program])    # If apple was typed, display a dialog:    elif userInput == "apple":        text="You chose apple."        dialog.info_dialog(title='Info', message=text)    # If banana was typed, display a dialog:    elif userInput == "banana":        text="You chose banana."        dialog.info_dialog(title='Info', message=text)    # If apple was typed, display a dialog:    elif userInput == "coconut":        text="You chose coconut."        dialog.info_dialog(title='Info', message=text)    # If anything else was typed or the default example string was accepted, display an invalid dialog:    else:        text = "You typed: " + userInput + "\n\nPlease enter a valid command."        dialog.info_dialog(title="Invalid", message=text, width="200")# If the exit code was 1, display a cancel dialog:elif retCode == 1:    text = "You pressed the Esc key or the Cancel button."    dialog.info_dialog(title="Cancelled", message=text, width="200") # If the exit code was anything other than 0 or 1, display an error dialog:else:    text = "Something went wrong."    dialog.info_dialog(title="Error", message=text, width="200")</code>

10、GUI日期对话框。

一个GUI日期选择对话框,等待用户选择日期,然后根据用户是取消/关闭窗口还是选择日期,使用对话框的返回代码显示两个不同对话框中的一个。日期的默认格式为YYYY-MM-DD。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript"># Create a variable for the return code and the date and put the chosen date's value into the date variable:retCode, date = dialog.calendar(title="Choose a date")# Use the following line instead if you'd like to control the format of the date:# retCode, date = dialog.calendar(title="Choose a date", format="%d-%m-%y")# If no date is chosen and the Cancel button is clicked, the Esc key is pressed, or the dialog window is closed:if retCode:    # Create a message and display it in a dialog:    myMessage = "No date was chosen."    dialog.info_dialog(title="Cancelled", message=myMessage, width="200")else:    # Display the value of the date variable in a dialog:    dialog.info_dialog(title="The date you chose is:", message=date, width="200")</code>

11、获取并打印剪贴板内容。

此脚本将剪贴板的内容(或剪贴板为空时的空字符串)放入变量中,并将变量的内容(如果不是空字符串)打印到当前活动的窗口中。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript"># Get the clipboard contents if the clipboard isn't empty:try:     # Get the contents of the clipboard and assign them to a variable:    cb = clipboard.get_clipboard()    # (Optional) Brief delay to make sure the clipboard has been filled in case you just filled it:    time.sleep(0.2)# Handle the exception if the clipboard is empty:except:     # Assign an empty string value to the variable:    cb = ""# Print the contents of the variable in the active window:keyboard.send_keys(cb)</code>

12、等待鼠标点击。

你可以让脚本等待鼠标点击,如果没有收到鼠标点击,可以使用计时器在指定的延迟后执行操作,也可以不使用计时器,在这种情况下,只有在实际点击鼠标后才会执行操作。

例如:打印文本前等待左键单击。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">mouse.wait_for_click(1)keyboard.send_keys("hello world")</code>

例如:打印文本前等待中键单击。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">mouse.wait_for_click(2)keyboard.send_keys("hello world")</code>

例如:在打印文本之前等待左键单击,或者如果没有左键单击发生,则在计时器用完时打印文本。

代码语言:javascript代码运行次数:0运行复制
<code class="javascript">mouse.wait_for_click(1, timeOut=3.0)keyboard.send_keys("hello world")</code>

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
视频后缀名都有哪些
视频后缀名都有哪些

视频后缀名都有avi、mpg、mpeg、rm、rmvb、flv、wmv、mov、mkv、ASF、M1V、M2V、MPE、QT、VOB、RA、RMJ、RMS、RAM、等等。更多关于视频后缀名的相关知识,详情请看本专题下面的文章,php中文网欢迎大家前来学习。

3886

2023.10.31

C++ Qt图形开发
C++ Qt图形开发

本专题专注于 C++ Qt框架在图形界面开发中的应用,系统讲解窗口设计、信号与槽机制、界面布局、事件处理、数据库连接与跨平台打包等核心技能,通过多个桌面应用项目实战,帮助学员快速掌握 Qt 框架并独立完成跨平台GUI软件的开发。

76

2025.08.15

C++ 图形界面开发基础(Qt方向)
C++ 图形界面开发基础(Qt方向)

本专题系统讲解 使用 C++ 与 Qt 进行图形界面(GUI)开发的核心技能,内容涵盖 Qt 项目结构、窗口组件、信号与槽机制、事件处理、布局管理、资源管理,以及跨平台编译与打包流程。通过多个小型桌面应用实战案例,帮助学习者掌握从界面设计到功能实现的完整 GUI 开发能力。

112

2025.12.05

chrome什么意思
chrome什么意思

chrome是浏览器的意思,由Google开发的网络浏览器,它在2008年首次发布,并迅速成为全球最受欢迎的浏览器之一。本专题为大家提供chrome相关的文章、下载、课程内容,供大家免费下载体验。

1068

2023.08.11

chrome无法加载插件怎么办
chrome无法加载插件怎么办

chrome无法加载插件可以通过检查插件是否已正确安装、禁用和启用插件、清除插件缓存、更新浏览器和插件、检查网络连接和尝试在隐身模式下加载插件方法解决。更多关于chrome相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

846

2023.11.06

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1570

2023.10.24

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

49

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 10.7万人学习

Git 教程
Git 教程

共21课时 | 4.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号