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运行复制
text = clipboard.get_selection()clipboard.fill_clipboard(text.lower().replace(' ', '-'))

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

Ke361开源淘宝客系统
Ke361开源淘宝客系统

Ke361是一个开源的淘宝客系统,基于最新的ThinkPHP3.2版本开发,提供更方便、更安全的WEB应用开发体验,采用了全新的架构设计和命名空间机制, 融合了模块化、驱动化和插件化的设计理念于一体,以帮助想做淘宝客而技术水平不高的朋友。突破了传统淘宝客程序对自动采集商品收费的模式,该程序的自动 采集模块对于所有人开放,代码不加密,方便大家修改。集成淘点金组件,自动转换淘宝链接为淘宝客推广链接。K

下载
代码语言:javascript代码运行次数:0运行复制
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)

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

例如:2024.10.13 23:47:00

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

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

例如:23:47:00

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

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

例如:2024.10.13

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

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

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

7、打开某个网站。

代码语言:javascript代码运行次数:0运行复制
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)

8、获取当前鼠标坐标。

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

代码语言:javascript代码运行次数:0运行复制
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

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

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

代码语言:javascript代码运行次数:0运行复制
################################################################ 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")

10、GUI日期对话框。

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

代码语言:javascript代码运行次数:0运行复制
# 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")

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

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

代码语言:javascript代码运行次数:0运行复制
# 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)

12、等待鼠标点击。

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

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

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

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

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

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

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

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

771

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

661

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

764

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

659

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1345

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

549

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

579

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

730

2023.08.11

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

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

精品课程

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

共48课时 | 7.6万人学习

Git 教程
Git 教程

共21课时 | 2.9万人学习

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

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