autohotkey

AutoHotkey
自动热键

AutoHotkey is a fantastic but complicated piece of software. It was initially intended to rebind custom hotkeys to different actions but is now a full Windows automation suite.

AutoHotkey是一款出色而复杂的软件。 它最初旨在将自定义热键绑定到不同的操作,但现在是完整的Windows自动化套件。

AHK isn’t particularly hard to learn for new users, as the general concept is fairly simple, but it is a full, Turing-complete programming language. You will pick up the syntax much easier if you have a programming background or are familiar with the concepts.

对于新用户来说,AHK并不难学习,因为它的总体概念相当简单,但是它是一种完整的,图灵完备的编程语言。 如果您具有编程背景或熟悉这些概念,则将更轻松地掌握语法。

安装和使用自动热键 (Installing and Using AutoHotkey)

AutoHotkey’s installation process is straightforward. Download the installer from the official website and run it. Choose “Express Installation.” After you’ve installed the software, you can right-click anywhere and select New > AutoHotkey Script to make a new script.

AutoHotkey的安装过程非常简单。 从官方网站下载安装程序并运行它。 选择“快速安装”。 安装软件后,可以右键单击任意位置,然后选择“新建”>“自动热键脚本”以创建新脚本。

AHK scripts are text files with a .ahk extension. If you right-click them, you’ll get a few options:

AHK脚本是扩展名为.ahk文本文件。 如果右键单击它们,将获得一些选项:

  • “Run Script” will load your script with the AHK runtime.“运行脚本”将使用AHK运行时加载脚本。
  • “Compile Script” will bundle it with an AHK executable to make an EXE file you can run.“编译脚本”将其与AHK可执行文件捆绑在一起,以使您可以运行EXE文件。
  • “Edit Script” will open your script in your default text editor. You can use Notepad to write AHK scripts, but we recommend using SciTE4AutoHotkey, an editor for AHK which supports syntax highlighting and debugging.

    “编辑脚本”将在您的默认文本编辑器中打开您的脚本。 您可以使用记事本编写AHK脚本,但是我们建议使用SciTE4AutoHotkey ,它是AHK的编辑器,支持语法高亮和调试。

While a script is running—whether it’s an EXE or not—you’ll find it running in the background in the Windows notification area, also known as the system tray. Look for the green icon with an “H” on it.

在脚本运行时(无论它是否是EXE),您都会在Windows通知区域(也称为系统托盘)的后台找到它。 查找上面带有“ H”的绿色图标。

To exit, pause, reload, or edit a script, right-click the notification icon and select an appropriate option. Scripts will continue to run in the background until you exit them. They’ll also go away when you sign out of Windows or reboot your PC, of course.

要退出,暂停,重新加载或编辑脚本,请右键单击通知图标并选择适当的选项。 脚本将继续在后台运行,直到您退出它们。 当然,当您退出Windows或重新启动PC时,它们也会消失。

AutoHotkey如何工作? (How Does AutoHotkey Work?)

At its core, AHK does one thing—bind actions to hotkeys. There are a lot of different actions, hotkey combinations, and control structures, but all scripts will operate on the same principle. Here’s a basic AHK script that launches Google Chrome whenever you press Windows+C:

AHK的核心是做一件事-将操作绑定到热键。 有许多不同的操作,热键组合和控制结构,但是所有脚本都将基于相同的原理进行操作。 这是一个基本的AHK脚本,只要您按Windows + C就会启动Google Chrome:

#c::
Run Chrome
return

The first line defines a hotkey. The pound sign (#) is short for the Windows key and c is the C key on the keyboard. After that, there’s a double colon (::) to signify the start of an action block.

第一行定义一个热键。 井号(#)是Windows键的缩写, c是键盘上的C键。 在那之后,有一个双冒号(::)表示操作块的开始。

The next line is an action. In this case, the action launches an application with the Run command. The block is finished with a return at the end. You can have any number of actions before the return. They will all fire sequentially.

下一行是一个动作。 在这种情况下,操作将使用“ Run命令启动应用程序。 块结束,最后return 。 在return之前,您可以执行任何操作。 他们都会按顺序射击。

Just like that, you’ve defined a simple key-to-action mapping. You can place as many of these as you’d like in a .ahk file and set it to run in the background, always looking for hotkeys to remap.

就像这样,您已经定义了一个简单的键到操作映射。 您可以在.ahk文件中放置任意.ahk文件,并将其设置为在后台运行,始终寻找要重新映射的热键。

热键和修饰符 (Hotkeys and Modifiers)

You can find a full list of AHK’s modifiers in official documentation, but we’ll focus on the most useful (and cool) features.

您可以在官方文档中找到AHK修饰符的完整列表,但我们将重点介绍最有用(最酷)的功能。

Modifier keys all have single character shorthands. For example, # ! ^ + are Windows, Alt, Control, and Shift, respectively. You can also differentiate between left and right Alt, Control, and Shift with the < and > modifiers, which opens up a lot of room for extra hotkeys. For example, <! is left Alt and >+ is right Shift. Take a look at the key list for everything you can reference. (Spoiler: You can reference nearly much every key. You can even reference other non-keyboard input devices with a small extension).

修饰键均具有单字符简写。 例如, # ! ^ + # ! ^ +分别是Windows,Alt,Control和Shift。 您还可以使用<>修饰符在左右Alt,Control和Shift之间进行区分,这为额外的热键打开了很大的空间。 例如,<! 左移Alt,> +右Shift。 查看所有可以参考的键列表 。 (扰流器:您几乎可以引用每个键。甚至可以引用其他带有小扩展名的非键盘输入设备)。

You can combine as many keys as you’d like into one hotkey, but you’ll soon run out of key combinations to remember. This is where modifiers, which let you do crazier things, come in. Let’s break down an example from the AHK docs:

您可以将任意数量的键组合到一个热键中,但是很快就会用不完要记住的组合键。 这是让您做更疯狂的事情的修饰符出现的地方。让我们从AHK文档中分解一个示例:

The green #IfWinActive is called a directive, and applies additional context to hotkeys physically under it in the script. Any hotkey after it will only fire if the condition is true, and you can group multiple hotkeys under one directive. This directive won’t change until you hit another directive, but you can reset it with a blank #If (and if that seems like a hack, welcome to AHK).

绿色的#IfWinActive被称为指令 ,并在脚本中物理地将附加上下文应用于它下面的热键。 此条件之后的任何热键仅在条件为真时才会触发,并且您可以在一个指令下将多个热键分组。 直到您按下另一个指令,该指令才会更改,但是您可以使用空白的#If重置它(如果这看起来像是hack,欢迎使用AHK)。

The directive here is checking if a specific window is open, defined by ahk_class Notepad. When AHK receives the input “Win+C,” it will fire the action under the first #IfWinActive only if the directive returned true, and then check the second one if it didn’t. AHK has a lot of directives, and you can find all of them in the docs.

此处的指令是检查是否打开了由ahk_class Notepad定义的特定窗口。 当AHK收到输入“ Win + C”时,只有在指令返回true时,它才会在第一个#IfWinActive下触发操作,然后检查第二个是否未执行。 AHK有很多指令,您可以在docs中找到所有指令。

AutoHotkey also has hotstrings, which function like hotkeys except replacing a whole string of text. This is similar to how autocorrect works—in fact, there’s an autocorrect script for AHK—but supports any AHK action.

AutoHotkey还具有hotstrings ,其功能类似于热键,只是替换了整个文本字符串。 这类似于自动更正的工作原理(实际上,AHK有一个自动更正脚本) ,但支持任何AHK操作。

The hotstring will match the string only if it’s typed exactly. It will automatically remove the matched text to replace the hotstring, too, although this behavior can be adjusted.

仅当键入正确的字符串时,hotstring才会匹配该字符串。 尽管可以调整此行为,它也会自动删除匹配的文本以替换热字符串。

动作 (Actions)

An action in AHK is anything that has an outside effect on the operating system. AHK has a lot of actions. We can’t possibly explain all of them, so we’ll pick out some useful ones.

在AHK中执行操作是对操作系统产生外部影响的任何事情。 AHK有很多行动。 我们可能无法解释所有这些信息,因此我们将挑选一些有用的信息。

  • Sending input, whether it’s text or various button presses.

    发送输入 ,无论是文本还是各种按钮。

  • Moving the mouse around. In fact, AHK is sometimes erroneously flagged as cheat software for video games, since people have made fully functioning aimbots with it.

    左右移动鼠标 。 实际上,AHK有时会被错误地标记为视频游戏的作弊软件,因为人们已经使用它制作了功能齐全的瞄准机器人。

  • Clicking the mouse, with positioning relative to the current window.

    单击鼠标 ,相对于当前窗口定位。

  • Displaying dialog menus, complete with forms and input fields.

    显示对话框菜单 ,其中包含表单和输入字段。

  • Moving windows around, adjusting size, and opening and closing.

    移动窗口 ,调整大小以及打开和关闭窗口 。

  • Playing music.

    播放音乐 。

  • Writing to the Windows Registry. Yes, really.

    写入Windows注册表 。 对真的。

  • Modifying the contents of the Clipboard.

    修改剪贴板的内容 。

  • Reading and writing files. You can loop through files and run actions on each line. AHK can even write to .ahk files and adjust its own code.

    读写文件 。 您可以循环浏览文件并在每一行上运行操作。 AHK甚至可以写入.ahk文件并调整其自己的代码。

Most of these actions will also have information-oriented commands associated with them. For example, you can write to the clipboard, but you can also get the contents of the Clipboard to store in a variable and run functions when the clipboard changes.

这些操作大多数还将具有与之相关的面向信息的命令。 例如,您可以写入剪贴板,但也可以获取剪贴板的内容以将其存储在变量中,并在剪贴板更改时运行函数。

通过控制结构将其全部捆绑 (Tying it All Up With Control Structures)

AHK wouldn’t be what it is without all of the control structures that make it Turing-complete.

如果没有所有使其成为Turing-complete的控制结构,AHK就不会是这样。

In addition to the #If directives, you also have access to If inside of action blocks. AHK has For loops, curly brace blocks, Try and Catch statements, and many others. You can access outside data from within the action block, and store it in variables or objects to use later. You can define custom functions and labels. Really, anything you could do easily in another programming language you can probably do in AHK with a bit of a headache and a look through the docs.

除了#If指令,您还可以访问操作块内部的If 。 AHK具有For循环, 花括号块, TryCatch语句等。 您可以从操作块中访问外部数据,并将其存储在变量或对象中以备后用。 您可以定义自定义功能和标签 。 确实,您可以轻松地用另一种编程语言完成的任何事情,甚至可能在AHK中也可以完成,有些麻烦,并且需要仔细阅读文档。

For example, imagine you have a boring, repetitive task that requires you to click multiple buttons in a row and wait for a server to respond before doing it over again ad infinitum. You can use AHK to automate this. You’d want to define a few loops to move the mouse to specific locations, click, and then move to the next spot and click again. Throw in a few wait statements to make it not break. You could even try to read the color of pixels on screen to determine what’s happening.

例如,假设您有一个令人厌烦的重复性任务,要求您连续单击多个按钮并等待服务器响应,然后再无限制地进行操作。 您可以使用AHK自动执行此操作。 您需要定义一些循环,以将鼠标移至特定位置,单击,然后移至下一个位置并再次单击。 放入一些等待语句以使其不中断。 您甚至可以尝试读取屏幕上像素的颜色来确定正在发生的事情。

One thing’s for certain—your script probably won’t be pretty. But neither is AutoHotkey, and that’s okay.

可以肯定的是,您的脚本可能不会很漂亮。 但是AutoHotkey都不是,这没关系。

翻译自: https://www.howtogeek.com/409581/how-to-write-an-autohotkey-script/

autohotkey

autohotkey_如何编写一个AutoHotkey脚本相关推荐

  1. 如何编写一个shell脚本

    本文结合大量实例阐述如何编写一个shell脚本. 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而 ...

  2. Python 在windows上跑图色脚本?简单又好玩,自己编写一个自动化脚本

    Python 在windows上跑图色脚本?简单又好玩,自己编写一个自动化脚本 大家好 我又来开新坑了,如图这次准备用python弄个简单脚本(根据图色判断进行键鼠操作) 1.老规矩 先安排运行环境 ...

  3. 编写一个shell脚本,可以在屏幕上打印出笛卡尔曲线

    您可以编写一个 shell 脚本来实现在屏幕上打印笛卡尔曲线.以下是一个示例脚本: #!/bin/bashfor ((y=0; y<=20; y++)) dofor ((x=0; x<=2 ...

  4. linux服务脚本书写,编写一个服务脚本的示例

    1.编写一个脚本[root@lomain tmp]# vim test.sh case $1 in start) echo "start.." ;; stop) echo &quo ...

  5. 如何写SHELL脚本?尝试自己编写一个简单脚本

    背景 现在多数的服务器都是Linux系统的,需要通过shell来进行操作,而利用shell脚本,可以大大提高开发维护的效率. 知识剖析 什么是shell shell是一个命令行解释器,它为用户提供了一 ...

  6. 编写一个shell脚本程序,检测指定IP地址的主机是否在线

    编写一个脚本程序,检测指定IP地址的主机是否在线,如果在线则显示输出Online,否则输出Offline checkhost.sh [root@exam ~]# cd scripts [root@ex ...

  7. 编写一个shell脚本,使其能够备份/etc目录下所有文件,并且备份的文件名需要自动生成日期,即产生后缀名形如.backup_20210624的文件。

    一.实现过程: 目录 一.实现过程: 二.验证结果: 1.在当前目录下,使用vi或者touch命令新建一个shell脚本文件,并且使用chmod命令添加权限(我这里添加的是最高权限),如图: 2.使用 ...

  8. 编写一个shell脚本,可以每周一凌晨1点钟执行 某个任务

    下面是一个示例脚本,它会在每周一的凌晨1点执行"某个任务": #!/bin/bashwhile true; docurrent_day=$(date +\%u)current_ho ...

  9. redchat怎么编写shell脚本_如何写shell脚本?尝试自己编写一个简单脚本

    1. 创建Shell脚本文件 2. 显示消息 3. 使用变量 4. 反引号 5. 重定向输入输出 6. 管道 7. 执行数学运算 8. 退出脚本 1. 创建脚本文件的时候,必须将文件的第一行指定要使用 ...

最新文章

  1. 【eclipse】快速调整eclipse背景和格式的方法
  2. apache ab test使用 单独安装ab和htpasswd
  3. android 定位 闪退_Android使用百度地图出现闪退及定位时显示蓝屏问题
  4. Create React App使用
  5. Java枚举的小用法
  6. i2c的时钟延展问题
  7. 解决scala_spark本地读取csv中文乱码问题
  8. Material Design之AppBarLayout总结
  9. Spring Boot 表单验证
  10. classmethod自己定制
  11. 【读书笔记《Android游戏编程之从零开始》】13.游戏开发基础(Paint 画笔)
  12. 整人c语言代码大全,(C语言整人代码大全.doc
  13. Spss的基本方法使用步骤
  14. CSS 美化checkbox
  15. Socket(网络编程)面试题
  16. CodeForces - 633 H Fibonacci-ish II(莫队+线段树)
  17. NO2:《人生七年》告诉我们的5条人生法则
  18. 一种使用随机抽样梯度下降算法来预估词汇量的方法
  19. 苏世民:顶尖领导者的52条法则
  20. Isomorphic:二叉树同构

热门文章

  1. Chrome浏览器更新
  2. 幼儿园计算机教案 认识画图,信息技术教案《认识画图》
  3. 多路复用技术(频分多路复用、时分多路复用和波分多路复用)
  4. SPFA单源最短路径算法
  5. scanf()函数的用法
  6. 排行前50的编程语言
  7. Ubuntu中使用RoboMongo实现MongoDB的可视化
  8. sis防屏蔽程序_智能化弱电工程屏蔽机房基础知识
  9. 链路聚合技术及其配置
  10. 软件生命周期管理系统ALM配置说明(二)