'How to encapsule the following AutoHotKey statements into functions?

I'm using Barrier to control a Windows machine from macOS, and I would like to remap Windows key to Command key to perform more efficient shortcuts.

Traditionally, simple one line statements (e.g. #c::Send ^c) will work, but have one shortcomming: if you hold pressing Command key then press "c" repeatly, you won't get repeating copy actions as expected, you will get one copy action and multiple typing of "c"s, which is annoying and not as natural as genuine "Command+C".

In order to have real "Command+C" & "Command+V" experience (& many other shortcuts perfectly remapped), I figured out a more complex (but working) apporach:

LWin & c::
    StateWindowsPressC := true
    Send {Ctrl Down}c
#If (StateWindowsPressC)
    ~*LWin Up::
        Send {Ctrl Up}
        StateWindowsPressC := false
    return
#If

LWin & v::
    StateWindowsPressV := true
    Send {Ctrl Down}v   
#If (StateWindowsPressV)
    ~*LWin Up::
        Send {Ctrl Up}
        StateWindowsPressV := false
    return
#If

Please note that variables like StateWindowsPressC and StateWindowsPressV should not be the same name or they will cause chaos.

These statements work, but have too many duplicate statements, is there any way to encapsule them into functions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source