Article directory
scripting basics
Although through window recognition and key mapping, you can do a lot of work, but AutoHotKey still provides some simple programming functions, and it can also be handy for some slightly complicated tasks. But it should be noted that AHK
there is a big difference in syntax between V1 and V2, and all the following content is applicable to the V2 version.
In ahk, by ;
making a single-line comment, by \**\
making a multi-line comment, and the multi-line comment symbol is only valid when it appears on the first line.
From a programming point of view, any programming language has variables, and so does AHK, but in AHK, variables do not need to be declared, and variable names are not case-sensitive. Although data types such as strings, numbers, and booleans are provided, different types can be freely converted, and when to use which type depends entirely on the situation. Among them, the value supports integer and floating-point types, and 0x
the integer at the beginning represents hexadecimal.
Combo recognition
Write a simple script below to warm up. The meaning of the following script is that when you click the capital key for different times continuously, the corresponding information will pop up
CapsLock::
{
global
if (A_ThisHotkey != A_PriorHotkey){
num := 1
return
}
num += 1
SetTimer MultiPresses, -500
Return
}
; 弹出连击次数
MultiPresses(){
global
MsgBox "点击" . num . "次"
num := 0 ; num清零
Return
}
Among them, global
it means that the global mode is turned on, so that num
this variable is not limited to a certain scope.
A_ThisHotkey
and A_PriorHotkey
are built-in variables, the former represents the most recently executed hotkey, and the latter represents the last executed hotkey.
So if
the statement means that if the button clicked this time is different from the last time, it means that only one click was made CapsLock
, so it will num
be set to 1, and then exit. Otherwise, it means that the last click is also CapsLock
, so num
add 1.
Finally, the function is called SetTimer
, indicating that if no key is pressed within 500 milliseconds, then MultiPresses
this function will be executed.
In MultiPresses
, one pops up MsgBox
, and the latter %num%
means to extract num
the value of this variable and finally reset num
the value.
The effect is
setTimer
setTimer
The function can be automatically called repeatedly within the specified time interval, and the method of use is
SetTimer Function, Period, Priority
The meaning of Function
the function being called Period
is related to its value
- greater than 0, the timer will automatically repeat until explicitly disabled by the script
- If it is less than 0, the timer will only run once. In the above case, -500 means that the Function will be called after 500ms, and then the timer will be deleted.
- If it is 0, delete this timer directly.
The last parameter Proirity
indicates the priority, if only two parameters are input, it will not be the turn Proirity
.
judgment and choice
In the above script, if
an expression is used, which is a necessary branch statement in almost all programming languages, expressing judgment, and the writing method is also highly similar. In AHK, the complete expression is as follows. If the curly braces are a single-line statement , the curly braces can be omitted.
if(){}
if(){}
else{}
In addition, AHK supports switch...case
statements whose syntax is of the form
switch SwitchValue, CaseSense
{
case CaseValue1:
Statements1
case CaseValue2a, CaseValue2b:
Statements2
default:
Statements3
}
Still taking combo as an example, the above formula can be rewritten as
CapsLock::
{
global
if (A_ThisHotkey != A_PriorHotkey){
num := 1
return
}
num += 1
SetTimer MultiPresses, 500
Return
}
MultiPresses(){
global
switch num{
case 2: MsgBox "您点击两次"
case 3: MsgBox "您点击三次"
case 4: MsgBox "您点击四次"
default: MsgBox "您点击许多次"
}
num := 0 ; num清零
Return
}