AutoHotKey Script Preliminary: Judgment and Selection

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 AHKthere 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 0xthe 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, globalit means that the global mode is turned on, so that numthis variable is not limited to a certain scope.

A_ThisHotkeyand A_PriorHotkeyare built-in variables, the former represents the most recently executed hotkey, and the latter represents the last executed hotkey.

So ifthe 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 numbe set to 1, and then exit. Otherwise, it means that the last click is also CapsLock, so numadd 1.

Finally, the function is called SetTimer, indicating that if no key is pressed within 500 milliseconds, then MultiPressesthis function will be executed.

In MultiPresses, one pops up MsgBox, and the latter %num%means to extract numthe value of this variable and finally reset numthe value.

The effect is

insert image description here

setTimer

setTimerThe function can be automatically called repeatedly within the specified time interval, and the method of use is

SetTimer Function, Period, Priority

The meaning of Functionthe function being called Periodis 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 Proirityindicates the priority, if only two parameters are input, it will not be the turn Proirity.

judgment and choice

In the above script, ifan 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...casestatements 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
}

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/130917676