autohotkey入门_AutoHotkey-入门

autohotkey入门

Introduction

介绍

I have published numerous articles and five-minute video Micro Tutorials here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles and videos has brief information describing where to download the product and how to install it. I have also answered many questions with a reference to AutoHotkey as a component of the solution. As I was about to write another article with an AutoHotkey program and the how-to-download-and-install information, I decided it would be better to publish a more comprehensive write-up on the process, including some background on the language.

我在Experts Exchange上发表了许多文章和五分钟的视频微型教程,其中介绍了使用称为AutoHotkey语言编写的程序/脚本。 这些文章和视频均包含简要信息,描述了在何处下载产品以及如何安装产品。 我还以AutoHotkey作为解决方案的组成部分回答了许多问题。 当我要写另一篇有关AutoHotkey程序以及如何下载和安装信息的文章时,我决定最好对过程进行更全面的撰写,包括有关语言的背景知识。

Background and History

背景和历史

AutoHotkey is a free (in all senses of the word), open source product, published under the GNU GPL (General Public License, Version 2, June 1991). It is a fork of the popular AutoIt language. AutoHotkey started out as a keyboard shortcuts and mouse clicks/movements capability (hence, "Hotkey" in the name), but has been enhanced into a robust programming/scripting language.

AutoHotkey是一种免费的 (从任何意义上来说) 开源产品,根据GNU GPL (通用公共许可证,版本2,1991年6月)发行。 它是流行的AutoIt语言的分支。 AutoHotkey最初是作为键盘快捷键和鼠标单击/移动功能(因此,其名称为“ Hotkey”)开始的,但已增强为一种强大的编程/脚本语言。

AutoHotkey itself has forked several times, with the release at AutoHotkey.com considered by devotees of the language to be the active/primary branch. If you'd like to learn more about the history of the language, I recommend The AutoHotkey Foundation and Our History pages at its website and the Wikipedia AutoHotkey article.

AutoHotkey本身已经分叉了好几次,发布时间为 该语言的奉献者认为AutoHotkey.com是活动/主要分支。 如果您想了解更多有关该语言的历史,我建议在其网站上阅读AutoHotkey Foundation和Our History页面,以及Wikipedia AutoHotkey文章 。

Download and Installation

下载及安装

With a decent computer and Internet connection, the download and installation of AutoHotkey should take less than a minute. At the time of publishing the most recent update to this article, here are the steps:

有了不错的计算机和Internet连接,AutoHotkey的下载和安装将花费不到一分钟的时间。 在发布本文的最新更新时,这些步骤如下:

(1) Visit the AutoHotkey site and click the Download button:

(1)访问AutoHotkey网站 ,然后单击“ 下载”按钮:

autohotkey入门_AutoHotkey-入门_第1张图片

Clicking that will give you this:

点击将为您提供:

autohotkey入门_AutoHotkey-入门_第2张图片

(2) As a new user, click the Download Current Version button. The exact dialog that you get from this depends on your browser and its settings. For example, you may see something like this:

(2)作为新用户,单击“ 下载当前版本”按钮。 从中获得的确切对话框取决于您的浏览器及其设置。 例如,您可能会看到以下内容:

autohotkey入门_AutoHotkey-入门_第3张图片

In any case, run the installer.

无论如何,请运行安装程序

(3) If you have UAC on, you will see this:

(3)如果启用了UAC ,将看到以下内容:

autohotkey入门_AutoHotkey-入门_第4张图片

Sidebar: If you're wondering how I captured the UAC dialog box, see my EE article, How to disable the secure desktop when User Account Control (UAC) prompts for elevation.

补充工具栏:如果您想知道如何捕获UAC对话框,请参阅我的EE文章, 当用户帐户控制(UAC)提示提升权限时如何禁用安全桌面 。

(4) If it is a new installation, clicking the Yes button on the UAC dialog will give you this:

(4)如果是新安装,则在UAC对话框中单击“ 是”按钮将为您提供:

autohotkey入门_AutoHotkey-入门_第5张图片

The Express Installation is a good choice for new AutoHotkey users.

对于新的AutoHotkey用户而言, 快速安装是一个不错的选择。

(5) If you already have AutoHotkey on the system, it will display the installed version and offer to upgrade it, such as this:

(5)如果系统上已经有AutoHotkey,它将显示已安装的版本并提出对其进行升级,例如:

autohotkey入门_AutoHotkey-入门_第6张图片

Close all running scripts before you click the Upgrade to button, although if you don't, it will warn you:

在单击“ 升级到”按钮之前,请关闭所有正在运行的脚本,尽管如果不这样做,则会警告您:

autohotkey入门_AutoHotkey-入门_第7张图片

(6) In a very short period of time (seconds, not minutes), you will see this Installation complete dialog:

(6)在很短的时间内(几秒钟,而不是几分钟),您将看到此“ 安装完成”对话框:

autohotkey入门_AutoHotkey-入门_第8张图片

That's it! You now have AutoHotkey installed on your computer. It owns the file extension AHK, which is a plain text file with the program in it. You may use whatever text editor you want to create an AHK source code file, even the built-in Notepad (just be sure to save it as a file type of AHK). Since AutoHotkey is associated with AHK files, double-clicking on one in Windows/File Explorer (or whatever file manager you use) will cause AutoHotkey to run that script.

而已! 您现在已经在计算机上安装了AutoHotkey。 它拥有文件扩展名AHK ,它是其中包含程序的纯文本文件。 您可以使用任何想要创建AHK源代码文件的文本编辑器,甚至是内置的记事本 (只需确保将其保存为AHK的文件类型)即可。 由于AutoHotkey与AHK文件相关联,因此在Windows / File Explorer(或您使用的任何文件管理器)中双击一个文件将使AutoHotkey运行该脚本。

Hello World

你好,世界

To get started, let's code the classic Hello World program in AutoHotkey. Open up whatever text editor you want and copy/paste this one line of code in it:

首先,让我们在AutoHotkey中编写经典的Hello World程序。 打开所需的任何文本编辑器,然后在其中复制/粘贴以下一行代码:

MsgBox,0,First AHK script,Hello World 

Save the file as HelloWorld.ahk (or whatever you want to call it, as long as the file type is AHK) and then double-click it in your file manager. AutoHotkey will execute it and you'll see this:

将文件另存为HelloWorld.ahk (或只要您想调用它,只要文件类型为AHK),然后在文件管理器中双击它。 AutoHotkey将执行它,您将看到以下内容:

autohotkey入门_AutoHotkey-入门_第9张图片

You'll likely use the MsgBox command often, so you may want to study its syntax. In fact, all the AutoHotkey commands and functions are well-documented, with complete syntax and good examples.

您可能会经常使用MsgBox命令,因此您可能需要研究其语法 。 实际上, 所有AutoHotkey命令和功能都有完整的文档说明,并具有完整的语法和良好的示例。

Compiler

编译器

A standard installation of AutoHotkey also installs a compiler, which converts an AHK source code file into a stand-alone executable (an EXE file). Note that it is not a "compiler" in the traditional sense, i.e., it does not compile the source code into object code. It simply bundles the AutoHotkey interpreter along with the source code of the script into an EXE file. That's why the "compiler" is actually called Convert .ahk to .exe (see below).

标准安装的AutoHotkey还会安装编译器 ,该编译器会将AHK源代码文件转换为独立的可执行文件( EXE文件)。 请注意,它不是传统意义上的“编译器”,即,它不会将源代码编译为目标代码。 它只是将AutoHotkey解释器以及脚本的源代码捆绑到EXE文件中。 这就是为什么“编译器”实际上称为将.ahk转换为.exe的原因 (请参见下文)。

The EXE files created by compiling typically run on all versions (and bit levels) of Windows going back to Windows 2000 (although there may be some limitations based on the Windows version). Indeed, I have run these EXE files on W2K, XP/32-bit, Vista/32-bit, W7/32-bit, W7/64-bit, and W8/64-bit. In addition, I just ran the HelloWorld.exe (created on W7) on the W10/64-bit Technical Preview (Build 10061, the latest release at the time of publishing this article) — it worked perfectly:

通过编译创建的EXE文件通常可以在Windows到Windows 2000的所有版本(和位级别)上运行(尽管基于Windows版本可能会有一些限制)。 的确,我已经在W2K,XP / 32位,Vista / 32位,W7 / 32位,W7 / 64位和W8 / 64位上运行这些EXE文件。 另外,我只是在W10 / 64位技术预览版(内部版本10061 ,发布本文时的最新版本)上运行了HelloWorld.exe (在W7上创建)–完美地运行了:

autohotkey入门_AutoHotkey-入门_第10张图片

The easiest way to compile is to right-click on an AHK file in your file manager and select Compile Script from the context menu:

编译的最简单方法是在文件管理器中右键单击AHK文件,然后从上下文菜单中选择“ 编译脚本 ”:

context-menu-Compile-Script.jpg

For more compiling options, such as selecting a custom icon or compressing the EXE file, you may run the compiler separately (the installer creates a shortcut to Convert .ahk to .exe in the AutoHotkey Program Group):

有关更多的编译选项,例如选择自定义图标或压缩EXE文件,可以单独运行编译器(安装程序会在AutoHotkey程序组中创建将.ahk转换为.exe的快捷方式):

autohotkey入门_AutoHotkey-入门_第11张图片

Notice in the context menu screenshot above that underneath the Compile Script item is an Edit Script item. This loads the script in whatever text editor you have configured for it. The default is Notepad, but you may change it by modifying this registry key:

HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Edit\Command

请注意,在上方的上下文菜单屏幕截图中,“ 编译脚本”项下面是“ 编辑脚本”项。 这会将脚本加载到您为其配置的任何文本编辑器中。 默认值为Notepad ,但是您可以通过修改以下注册表项来更改它:

HKEY_CLASSES_ROOT \ AutoHotkeyScript \ Shell \ Edit \ Command

For example, to change it to Notepad++ (assuming the standard installation location for it), modify the value of that key to this:

例如,要将其更改为Notepad ++ (假定它为标准安装位置),请将该键的值修改为此:

"C:\Program Files\Notepad++\notepad++.exe" "%1" “ C:\ Program Files \ Notepad ++ \ notepad ++。exe”“%1”

However, rather than modifying the registry, you may simply open the AHK file with your preferred text editor by using Windows/File Explorer or whatever file manager you use (that's what I do).

但是,您无需修改​​注册表,而只需使用Windows / File Explorer或您使用的任何文件管理器,使用首选的文本编辑器打开AHK文件(这就是我的工作)。

Component Object Model (COM)

组件对象模型(COM)

One of the most powerful AutoHotkey features is its native/built-in support for the Component Object Model (COM). It is extremely valuable in creating automation scripts for use with Microsoft Office apps, such as Excel and Word (similar to what can be done with VBA). The documentation begins at the ComObjActive() call and runs through the ComObjValue() call. Here's a small (working) script that shows Excel COM usage (of course, change the InputFile value to test it on your system):

AutoHotkey最强大的功能之一是其对组件对象模型COM )的本机/内置支持。 在创建用于Microsoft Office应用程序(例如ExcelWord)的自动化脚本时,这非常有价值(类似于VBA可以完成的工作)。 该文档始于ComObjActive()调用,并贯穿ComObjValue()调用。 这是一个小的(正在运行的)脚本,用于显示Excel COM的用法(当然,更改InputFile值以在系统上对其进行测试):

InputFile:="c:\temp\test.xlsx"
WorksheetNames:=""
oWorkbook:=ComObjGet(InputFile)
WorkheetsCount:=oWorkbook.Worksheets.Count
Loop,%WorkheetsCount%
  WorksheetNames:=WorksheetNames . oWorkbook.Worksheets(A_Index).Name . "`n"
MsgBox,4160,Worksheet Names,Number of Worksheets: %WorkheetsCount%`n`n%WorksheetNames%`n`n
ExitApp 

Using the MsgBox command discussed earlier, it displays the number of worksheets in the Excel file and shows each worksheet name. Here's a screenshot of sample output:

使用前面讨论的MsgBox命令,它可以在Excel文件中显示工作表的数量,并显示每个工作表的名称。 这是示例输出的屏幕截图:

autohotkey入门_AutoHotkey-入门_第12张图片

Graphical User Interface (GUI)

图形用户界面(GUI)

Another very powerful AutoHotkey feature is its ability to create programs with a Graphical User Interface (GUI). The documentation on it is quite good, with many examples. Here's a modification of Example #2 there with some enhancements:

AutoHotkey的另一个非常强大的功能是它具有使用图形用户界面GUI )创建程序的能力。 关于它的文档非常好,有很多示例。 这是示例2的修改,其中进行了一些增强:

Menu,FileMenu,Add,E&xit,FileMenuExit
Menu,MenuBar,Add,&File,:FileMenu
Menu,HelpMenu,Add,&About,HelpMenuAbout
Menu,MenuBar,Add,&Help,:HelpMenu
Gui,Menu,MenuBar
Gui,Add,Text,,First name:
Gui,Add,Text,,Last name:
Gui,Add,Edit,vFirstName ym
Gui,Add,Edit,vLastName
Gui,Add,Radio,vAgeRange xm,&Under 21
Gui,Add,Radio,Checked x+20,&21-65
Gui,Add,Radio,x+20,Over &65
Gui,Add,Button,xm Default,&OK
Gui,Add,Button,x+20,&Cancel
Gui,Show,,GUI Test
Return

ButtonOK:
Gui,Submit
MsgBox,4160,GUI Example,First Name: %FirstName%`nLast Name: %LastName%`nAge Range: %AgeRange%
ButtonCancel:
FileMenuExit:
GuiClose:
GuiEscape:
ExitApp

HelpMenuAbout:
Gui,+OwnDialogs
MsgBox,4160,About,Version 1.0
Return 

The script shows these GUI features:

该脚本显示了以下GUI功能:

Text

文字

• Data entry (Edit) fields

•数据输入( 编辑 )字段

Menu bar with File and Help menus

•带有文件帮助菜单的菜单

File menu has Exit pick

文件菜单具有退出选择

Help menu has About pick

帮助菜单中有关于选择

About dialog box is modal

关于对话框是模态的

• All menu (and submenu) picks have keyboard shortcuts (underlined in the screenshot below)

•所有菜单(和子菜单)选择都有键盘快捷键 (在下面的屏幕截图中有下划线)

Radio buttons in a 3-button group (also with underlines)

•3按钮组中的单选按钮(也带有下划线)

OK and Cancel buttons (also with underlines)

确定取消按钮(也带有下划线)

Here's what it looks like when you run it on a Windows 10 system:

这是在Windows 10系统上运行时的外观:

autohotkey入门_AutoHotkey-入门_第13张图片

It's extremely simplistic, but shows the power of GUI in a small amount of code.

它非常简单,但是用少量代码显示了GUI的功能。

Sidebar: The underlines on the first letter of the menus and buttons (and the underlines on the File and Help submenu items) are because that feature has been enabled in Windows (it is disabled by default). Here's how to enable it:

边栏:菜单和按钮的第一个字母下划线(以及“文件”和“帮助”子菜单项上的下划线)是因为该功能已在Windows中启用(默认情况下处于禁用状态)。 启用方法如下:

• Run Control Panel

•运行控制面板

• Open Ease of Access Center

•开放易用的访问中心

• Click Make the keyboard easier to use

•单击使键盘更易于使用

• Scroll down and tick the box that says Underline keyboard shortcuts and access keys

•向下滚动并勾选显示下划线键盘快捷键和访问键的框

End Sidebar 侧边栏 Other Resources 其他资源

For more examples of AutoHotkey code in full, working scripts, please see the many articles and five-minute video Micro Tutorials that I published here at EE. This is the current list:

有关完整的有效脚本中的AutoHotkey代码的更多示例,请参见我在EE上发布的许多文章和五分钟的视频微型教程。 这是当前列表:

Articles

文章

Test IP Addresses with PING
Answer Skype Calls with Bluetooth Headset
How to copy the short link of an ID at Experts Exchange to the clipboard with a single keystroke

使用PING测试IP地址
使用蓝牙耳机接听Skype呼叫
如何通过一次按键将Experts ID的短链接复制到剪贴板

How to solve the problem of incorrect System Uptime being reported when a system has been up for a long time
How to create an on/off toggle to mute the system audio/sound with a single mouse click or single keystroke

如何解决系统长时间启动后报告的系统正常运行时间不正确的问题
如何创建一个开/关切换以通过单击一次鼠标或一次击键将系统音频/声音静音

How to download number of Views, Endorsements, Points for Experts Exchange Articles and Videos

如何下载观点,认可,专家要点的数量交换文章和视频

How to move all windows on one monitor to another monitor with a single keystroke

如何通过一次按键将一台显示器上的所有窗口移动到另一台显示器上

AutoHotkey Switch-Case

自动热键开关盒

Keyboard shortcuts (hotkeys) to move mouse to monitors in a multi-monitor configuration - AutoHotkey Script

键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本

Exclude words when converting to "Title Case" - AutoHotkey Script

转换为“标题大小写”时排除单词-AutoHotkey脚本

Automatically download files from the web - AutoHotkey Script

从网上自动下载文件-AutoHotkey脚本

Videos

影片

On-Screen Display for CapsLock State (On or Off)
On-Screen Display for CapsLock State (On or Off) - Enhanced
Check for and download updated file - Example - FileZilla
How to download number of Views, Endorsements, Points for Experts Exchange Articles and Videos--Demo
ArticlesVideosEE: Download statistics on Experts Exchange Articles and Videos - Demo of Enhancements

CapsLock状态的屏幕显示(打开或关闭)
CapsLock状态的屏幕显示(打开或关闭)-增强
检查并下载更新的文件-示例-FileZilla
如何下载观点,认可,专家交流文章和视频的点数-演示
ArticlesVideosEE:下载有关专家交流文章和视频的统计信息-增强功能演示

Conclusion

结论

My purpose in writing this article is to provide an easy, quick, yet comprehensive Getting Started guide to which I can refer EE members. Going into more detail than presented in this article is not necessary, as there is an excellent, thorough AutoHotkey Beginner Tutorial at the AutoHotkey site, along with my articles and videos mentioned above.

我写这篇文章的目的是提供一个简单,快速但全面的《 入门指南》 ,我可以参考EE成员。 没有必要比本文中介绍的内容更详细,因为AutoHotkey站点上有一个非常详尽的AutoHotkey初学者教程 ,以及上面提到的我的文章和视频。

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

如果您发现本文有帮助,请单击下面的大拇指图标。 这使我知道什么对EE成员有价值,并为以后的文章提供了指导。 非常感谢! 问候乔

翻译自: https://www.experts-exchange.com/articles/18346/AutoHotkey-Getting-Started.html

autohotkey入门

你可能感兴趣的:(python,java,linux,编程语言,人工智能)