V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nexklee
V2EX  ›  程序员

AHK v2 为啥这里一定要静态变量?

  •  
  •   nexklee · 100 天前 · 770 次点击
    这是一个创建于 100 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Label1() { Send "{Numpad1}" ; Return }

    XButton2:: ; { static v_Enable := False
    If (v_Enable := !v_Enable) { SetTimer Label1, 0
    } Else { SetTimer Label1, 600 } return }

    代码见上,ahk V2 能正常运作,但第 9 行那个 static (静态变量)我省去就没有反应 非要加上 有什么办法不加那个静态标识吗

    非程序员 三脚猫 v1 熟一点 v2 摸着石头过河 很多不懂

    8 条回复    2024-01-18 10:54:53 +08:00
    nexklee
        1
    nexklee  
    OP
       100 天前
    哭了 这排版都是啥呀 哪里还有什么第 9 行 /捂脸
    nexklee
        2
    nexklee  
    OP
       100 天前
    FYFX
        3
    FYFX  
       100 天前
    > A static variable may be initialized on the same line as its declaration by following it with := and any expression. For example: static X:=0, Y:="fox". Static declarations are evaluated the same as local declarations, except that after a static initializer (or group of combined initializers) is successfully evaluated, it is effectively removed from the flow of control and will not execute a second time.
    看 ahkv2 的文档,加了 static 的行只会执行一次
    FYFX
        4
    FYFX  
       100 天前
    你把 v_Enable 放到外面初始化也是行的
    你这个 if(v_Enable := !v_Enable){...} 写法也挺魔法的。。。
    nexklee
        5
    nexklee  
    OP
       100 天前
    @FYFX #3
    我那句是想当一个开关 按 XButton2 切换 一次非 一次是 是就按键 1 非就什么都不做 需要只执行一次吗 局部变量不行?
    nexklee
        6
    nexklee  
    OP
       100 天前
    @FYFX #4

    v_Enable := False


    XButton2:: ;
    {
    v_Enable := !v_Enable
    If (v_Enable := False)
    {
    SetTimer Label1, 0 ;
    }
    Else
    {
    SetTimer Label1, 600 ;
    }
    return
    }

    这是我最早的 但 V2 版报错
    FYFX
        7
    FYFX  
       100 天前
    那里面可能要加一下 global v_Enable 声明,看你报错怎么说的
    nexklee
        8
    nexklee  
    OP
       100 天前
    @FYFX #7

    global 可以 ,static 也可以

    v_Enable := False


    XButton2:: ;
    {
    v_Enable := !v_Enable
    If (v_Enable := False)
    ...

    没有反应




    XButton2:: ;
    {
    v_Enable := False
    v_Enable := !v_Enable
    If (v_Enable := False)
    ...
    没有反应


    XButton2:: ;
    {
    static v_Enable := False
    If (v_Enable := !v_Enable)
    {
    ...

    这种最奇葩的样子 成功
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1615 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:42 · PVG 00:42 · LAX 09:42 · JFK 12:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.