搜档网
当前位置:搜档网 › AutoHotKey使用教程

AutoHotKey使用教程

AutoHotKey使用教程
AutoHotKey使用教程

使用教程(一)-----何为AutoHotKey?

AutoHotKey是一个免费开源的小软件,很小(只有1964 KB),但是功能却非常强大,它几乎可以做键盘、鼠标和操纵杆可以做的任何事情。它可以把人力完全的释放出来,前提只要你编写一个后缀为.ahk的脚本。如果你愿意,你完全可以把你写的script理解为一个机器人,它可以做你想让它做的任何事情。

AutoHotKey在国外使用的很普遍,国内也有很大的用户群,大家只要Google 一下就知道了,AutoHotKey之前的版本是AutoIt2,现在newest的版本是AutoIt3,但是AutoHotKey还是一个比较经典的版本,最新的版本是,建议大家下载一下,有什么使用心得,多交流哈,呵呵。我会定期在我的博客上更新使用心得,大家多关注啊。

AutoHotKey使用教程(二)-----一个简单的例子

使用AutoHotKey编写脚本非常简单,现在我们就来开始编写我们的第一个简单的脚本吧。

使用notepad或者其他你熟悉的文本编辑器,新建一个文件,使用.ahk作为后缀名,例如。把下面的代码拷贝进去,保存退出,双击执行一下。

代码:

MsgBox,你好,欢迎关注风追月博客 `n 请继续关注哈

run, 看到什么结果了吗?神奇吗?简单吗?呵呵

如果感兴趣的话,我还会在后面的章节中,更加详细得讲解。

AutoHotKey使用教程(三)-----定义属于自己的热键

通过本节的学习,你可以自定义热键快速启动常用的程序、文档、目录、网址和WINDOWS的常用功能(如:增大\减小音量、系统静音开关、弹出\关闭光驱、打开控制面板模块属性等等)。在日常的工作中,它的确可以帮助您节省不少鼠标频繁点击的时间,从而大大的提高工作效率。

我们先来看一段简单的代码:

#n::

Run Notepad

return

在上面的代码中,"#"代表windows键(一种修饰符), "return"表示结束热键定义。如果一个热键定义中只有一个语句,如上面的例子,那么这条语句就可以放在::的后面,不需要添加"return":

#n:: Run Notepad

如果热键包含多个按键,组合键连续的放在一起(不需要考虑顺序),下面的例子即使用^!来代替Control+Alt :

^!s::

Send Sincerely,{enter}John Smith ; This line sends keystrokes to the active (foremost) window.

return

你可以同时定义多个热键,它们触发相同的事件,只要把热键“垂直”定义即可。

^Numpad0::

^Numpad1::

MsgBox Pressing either Control+Numpad0 or Control+Numpad1 will display this message.

return

如果你要禁用某个热键,非常简单,只要在return之前不写任何语句即可,下面的例子就禁用的右边的Windows键

RWin::return

AutoHotKey使用教程(四)----一些常用的修饰符

AutoHotKey使用教程(五)----按键重映射

按键重映射的语法格式:OriginKey::DestinationKey。请看下面的例子:a::b

在上面的例子中体会以下几点:

1)使用键盘敲“a“,会自动映射成”b“

2)敲”Shift + a “ 呢? B

3)敲”capslock + a “ 呢? B

4)敲”b“ 呢?还是b

a::B

1) 使用键盘敲“a“,会自动映射成”B“

2) 敲”Shift + a “ 呢? B

3) 敲”capslock + a “ 呢? b

总结:”Shift + a “ 跟“a“的效果是一样的,”capslock + a “是按映射后在转换大小写。

鼠标的重映射语法跟按键重映射一致。

RAlt::RButton ; Makes the right Alt key behave like the right mouse button.

RWin::Return ; Disables the right Windows key by having it simply return

使用#IfWinActive/Exist语句可以控制重映射生效的窗口。

#IfWinActive ahk_class Notepad

a::b ; Makes the 'a' key send a 'b' key, but only in Notepad.

#IfWinActive ; This puts subsequent remappings and hotkeys in effect for all windows.

一段使用键盘控制鼠标的代码,大家有兴趣可以试一试。

*#up::MouseMove, 0, -10, 0, R ; Win+UpArrow hotkey => Move cursor upward *#Down::MouseMove, 0, 10, 0, R ; Win+DownArrow => Move cursor downward *#Left::MouseMove, -10, 0, 0, R ; Win+LeftArrow => Move cursor to the left

*#Right::MouseMove, 10, 0, 0, R ; Win+RightArrow => Move cursor to the right

*<#RCtrl:: ; LeftWin + RightControl => Left-click (hold down

Control/Shift to Control-Click or Shift-Click).

SendEvent {Blind}{LButton down}

KeyWait RCtrl ; Prevents keyboard auto-repeat from repeating the mouse click.

SendEvent {Blind}{LButton up}

return

*<#AppsKey:: ; LeftWin + AppsKey => Right-click

SendEvent {Blind}{RButton down}

KeyWait AppsKey ; Prevents keyboard auto-repeat from repeating the mouse click.

SendEvent {Blind}{RButton up}

return

AutoHotKey使用教程(六)-----鼠标和键盘按键操作列表

鼠标支持以下操作:

LButton RButton MButton WheelDown WheelUp

键盘按键支持以下操作:

Space - the spacebar

Tab

Enter (or Return)

Escape (or Esc) Backspace (or BS)

Delete (or Del)

Insert (or Ins)

Home

End

PgUp

PgDn

Up

Down

Left

Right

ScrollLock

CapsLock

NumLock

Numlock ON Numlock OFF Numpad0NumpadIns Numpad1NumpadEnd Numpad2NumpadDown Numpad3NumpadPgDn Numpad4NumpadLeft Numpad5NumpadClear Numpad6NumpadRight Numpad7NumpadHome

Numpad8NumpadUp

Numpad9NumpadPgUp

NumpadDot (.) NumpadDel

NumpadDiv (/)NumpadDiv (/)

NumpadMult (*)NumpadMult (*)

NumpadAdd (+)NumpadAdd (+)

NumpadSub (-)NumpadSub (-)

NumpadEnter NumpadEnter

1 a A LWin

Control (or

Ctrl) Alt Shift

F1

PrintScreen CtrlBreak Pause

Break

AutoHotKey使用教程(七)-----AutoHotkey脚本

AutoHotkey跟tcl等语言一样,是一种解释性语言。从脚本的头部至结尾顺序执行,除非遇到return,exit或者热键、热字符串。

一个AutoHotkey脚本如果不包含热键、热字符串、OnMessage或者 GUI等,就会在文件执行完成后推出,否则脚本会处于idle状态以备相应热键、热字符串等操作。

`的用法类似于c和tcl中的“\”,例如`t (tab), `n (linefeed), and `r (carriage return).

Autohotkey中逗号和百分号都有专门的意义,如果要表示字符意义,可以使用`, `%。

MsgBox This is ok.

MsgBox, This is ok too (it has an explicit comma).

AutoHotkey脚本使用“;”注释,“;”跟在需注释的语句之后,注意语句和分号之间只要要有一个空格。如果要注意多个语句可以使用/* */

Run Notepad ; This is a comment on the same line as a command.

/*

MsgBox, This line is commented out (disabled).

MsgBox, This one too.

*/

为了增加脚本的可读性和可维护性,有时需要将一个较长的语句拆分成几个较小的语句,可以使用以下几种方法:

1)一行以"and", "or", ||, &&或者逗号开始,会自动跟上一句组合成一句。

FileAppend, This is the text to append.`n ; A comment is allowed here.

, %A_ProgramFiles%\SomeApplication\ ; Comment.

if (Color = "Red" or Color = "Green" or Color = "Blue" ; Comment.

or Color = "Black" or Color = "Gray" or Color = "White") ; Comment.

and ProductIsAvailableInColor(Product, Color) ; Comment.

2)这种方法适用于要连接很多行并且方法1不适用的情况。

; EXAMPLE #1:

Var =

(

Line 1 of the text.

Line 2 of the text. By default, a linefeed (`n) is present between lines.

)

; EXAMPLE #2:

FileAppend, ; The comma is required in this case.

(

A line of text.

By default, the hard carriage return (Enter) between the previous line and this one will be written to the file as a linefeed (`n).

By default, the tab to the left of this line will also be written to the file (the same is true for spaces).

By default, variable references such

把所有要组合在一起的行放在一个圆括号内。

可以使用ahk2exe工具把Script 转换成 EXE文件(可以选用自己喜欢的图标)。也可以右键.ahk文件,选“Compile Script”进行编译。也可以使用命令行:

/in [/out ][/icon ][/pass password][/NoDecompile]

Pikka注意了一下,编译后文件会变的比较大。

AutoHotkey脚本允许设置断点,在需要设置断点的地方放上下面两句话就可以了,脚本运行到这个位置就会打印当前的变量。

ListVars

Pause

AutoHotKey使用教程(八)-----变量和表达式

AutoHotkey没有显示地定义一些变量类型,所有的变量都是字符串类型。

AutoHotkey里面的所有的变量都是全局变量,能被文件内任何位置引用。除非在函数里面声明的,是局部变量,只能在函数内部引用。AutoHotkey的变量不需要声明直接使用。

变量的名字不区分大小写,长度最长254个字母。

变量的存储有两种方法:

方法1:使用“=”去定义数字、没带引号的字符串和带%的变量

MyNumber = 123

MyString = This is a literal string.

CopyOfVar = %Var% ; With the = operator, percent signs are required to retrieve a variable's contents.

方法2:使用“:=”去定义数字,带引号的字符串和不带%的变量

MyNumber := 123

MyString := "This is a literal string."

CopyOfVar := Var ; Unlike its counterpart in the previous section, percent signs are not used with the := operator.

同样的,变量的引用也有两种方法:

方法1:

Var = 123

MsgBox The value in the variable named Var is %Var%.

方法2:

var := 123

MsgBox % "The value in the variable named Var is " . Var . ""

表达式的两个例子

NetPrice := Price * (1 - Discount/100)

if (CurrentSetting > 100 or FoundColor <> "Blue")

MsgBox The setting is too high or the wrong color is present.

AutoHotkey支持的运算符包括:%Var%、++ --、**、- ! ~ & *、*

/ 、> < >= <=、= == <> !=、NOT、AND &&、OR ||、?:、:= += -= *= /= |= &= ^= >>= <<=、, 、 mod()、round()、abs()

下面这些变量是内嵌在程序中,有特殊的含义,任何脚本都可以引用它们,只能读不能写。

Special Characters: A_Space, A_Tab

Script Properties: command line parameters, A_WorkingDir,

A_ScriptDir, A_ScriptName, (...more...)

Date and Time: A_YYYY, A_MM, A_DD, A_Hour, A_Min, A_Sec, (...more...)

Script Settings: A_IsSuspended, A_BatchLines, A_TitleMatchMode, (...more...)

User Idle Time: A_TimeIdle, A_TimeIdlePhysical

GUI Windows and Menu Bars: A_Gui, A_GuiControl, A_GuiEvent,

A_EventInfo

Hotkeys, Hotstrings, and Custom Menu Items:A_ThisHotkey, A_EndChar, A_ThisMenuItem, (...more...)

Operating System and User Info: A_OSVersion, A_ScreenWidth,

A_ScreenHeight, (...more...)

Misc: A_Cursor, A_CaretX, A_CaretY, Clipboard, ClipboardAll, ErrorLevel

Loop: A_Index, (...more...)

AutoHotKey使用教程(九)-----函数

一个函数的简单的例子:

Add(x, y)

{

}

函数调用时,变量的后面使用:=

Var := Add(2, 3) ;

由于一个函数调用就是一个表达式,所以函数的参数列表里面的变量前面不能加百分号“%”,相反的,字符串要使用引号,请看下面的例子:

if InStr(MyVar, "fox")

MsgBox The variable MyVar contains the word fox.

当一个命令的参数调用函数的时候,如果命令的参数不支持表达式,就必须使用百分号“%”,请看下面的例子:

MsgBox % "The answer is: " . Add(3, 2)

一个函数可以带参数也可以不带参数(类似C中的过程)。

函数的参数列表里的变量是局部变量,除非前面增加修饰符“ByRef”。

Swap(ByRef Left, ByRef Right)

{

temp := Left

Left := Right

Right := temp

}

在上面的例子中,参数列表里的变量前面加了修饰符“ByRef”,这样参数就成为了调用者传递的变量的引用,参数和调用者的变量就指向了内存中的同一块内容。(类似C中的传地址调用,如果不加修饰符“ByRef”,就是传值调用)

使用“ByRef”还可以使函数返回多于一个返回值。

AutoHotkey 函数支持可选参数,只要在可选参数的后面加个等号即可。

Add(X, Y, Z = 0)

{

return X + Y + Z

}

如果给函数传递了3个值,可选参数就被忽略,否则就使用默认值。

AutoHotkey里面也有局部变量,全局变量,静态变量的概念,涵义跟C语法相同,用法类似,下面主要讲一下不同点:

函数内部在要使用全局变量的之前,必须再声明一下。

LogToFile(TextToLog)

{

global LogFileName ; This global variable was previously given a value somewhere outside this function.

FileAppend, %TextToLog%`n, %LogFileName%

}

如果有较多的全局变量,就可以在第一行使用global,这样就认为后面的变量都是全局变量。

SetDefaults()

{

global ; This word can be omitted if the first line of this function will be something like "local MyVar".

Var := 33 ; Assigns 33 to a global variable, first creating the variable if necessary.

local x, y:=0, z ; Local variables must be declared in this mode, otherwise they would be assumed global.

; ...and so on.

}

在定义变量的时候,同种类型的变量之间使用逗号隔开

global LogFileName, MaxRetries := 5

static TotalAttempts = 0, PrevResult

可以使用“#include”从外部文件引用函数。(是不是跟C很相似呢,呵呵)

函数使用return返回一个结果推出,也可以使用exit从当前线程中终止退出。

AutoHotKey使用教程(十)---Hotstring使用介绍

“Hotstring”我也不知道怎么翻译,姑且直译为“热字符串”吧。

热字符串主要用来扩展缩写的功能(本质就是替换),当然你也可以用它来触发一些动作,在这一点上,它跟热键没有区别。

::btw::by the way

在上面的例子中,缩写“btw”会自动的替换成“by the way”。(注意,在敲击btw之后,你要增加一个结束符,例如空格,回车和逗号等符号。

下面是一个热字符串用来触发动作的例子。

::btw::

MsgBox You typed "btw".

return

使用#IfWinActive/Exist可以实现环境敏感的热字符串,例如下面的例子

#IfWinActive ahk_class Notepad

::btw::This replacement text will appear only in Notepad.

#IfWinActive

::btw::This replacement text appears in windows other than Notepad.

如果要想在热字符串替换后加上空格,可以在语句的后边加上`,例如:

:*:btw::By the way `

结束符说明:

1)默认的结束符包括:-()[]{}':;"/\,.?!`n `t

2)`n is Enter, `t is Tab

3)注意上面的`n `t之间有个空格

4)用户可以自己定义结束符,方法如下:

#Hotstring EndChars -()[]{}:;'"/\,.?!`n `t

选项说明:

可以通过修改选项修改Hotstring的默认行为,可以通过下面的两种方式修改(1)使用#Hotstring修改,它的作用域从当前位置开始到文件结束为止。

#Hotstring r c ; All the below hotstrings will use "send raw" and will be case sensitive by default.

::al::airline

::CEO::Chief Executive Officer

(2)把选项添加在第一对冒号的中间,只对本条Hotstring生效 ; Case sensitive and "ending character not required".

(2)常用选项

* :不需要终止符去触发Hotstring,使用*0选项关闭

?:即使热字符串在另一个单词的中间,依然可以触发。关闭使用?0

C :大小写敏感,关闭使用c0

O :当热字符串生效时,不打印使用的终止符

Tips:

下面是Andreas Borutta写的一个小脚本,你可以使用它定义自己的热键,只要是选中一个单词,然后使用“windows +H”快捷键即可,它会自动即时加载热键。

#h:: ; Win+H hotkey

; Get the text currently selected. The clipboard is used instead of

; "ControlGet Selected" because it works in a greater variety of editors ; (namely word processors). Save the current clipboard contents to be ; restored later. Although this handles only plain text, it seems better ; than nothing:

AutoTrim Off ; Retain any leading and trailing whitespace on the clipboard.

ClipboardOld = %ClipboardAll%

Clipboard = ; Must start off blank for detection to work.

Send ^c

ClipWait 1

if ErrorLevel ; ClipWait timed out.

return

; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:

; The same is done for any other characters that might otherwise

; be a problem in raw mode:

StringReplace, Hotstring, Clipboard, ``, ````, All ; Do this replacement first to avoid interfering with the others below. StringReplace, Hotstring, Hotstring, `r`n, ``r, All ; Using `r works better than `n in MS Word, etc.

StringReplace, Hotstring, Hotstring, `n, ``r, All

StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All

StringReplace, Hotstring, Hotstring, `;, ```;, All

Clipboard = %ClipboardOld% ; Restore previous contents of clipboard. ; This will move the InputBox's caret to a more friendly position: SetTimer, MoveCaret, 10

; Show the InputBox, providing the default hotstring:

InputBox, Hotstring, New Hotstring, Type your abreviation at the indicated insertion point. You can also edit the replacement text if you wish.`n`nExample entry: :R:btw`::by the way,,,,,,,, :R:`::%Hotstring% if ErrorLevel ; The user pressed Cancel.

return

IfInString, Hotstring, :R`:::

{

MsgBox You didn't provide an abbreviation. The hotstring has not been added.

return

}

; Otherwise, add the hotstring and reload the script:

FileAppend, `n%Hotstring%, %A_ScriptFullPath% ; Put a `n at the beginning in case file lacks a blank line at its end.

Reload

Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.

MsgBox, 4,, The hotstring just added appears to be improperly formatted. Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.

IfMsgBox, Yes, Edit

return

MoveCaret:

相关主题