AHK v2 为啥这里一定要静态变量? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
请不要在回答技术问题时复制粘贴 AI 生成的内容
nexklee

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

  •  
  •   nexklee Jan 18, 2024 1946 views
    This topic created in 832 days ago, the information mentioned may be changed or developed.

    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 replies    2024-01-18 10:54:53 +08:00
    nexklee
        1
    nexklee  
    OP
       Jan 18, 2024
    哭了 这排版都是啥呀 哪里还有什么第 9 行 /捂脸
    nexklee
        2
    nexklee  
    OP
       Jan 18, 2024
    FYFX
        3
    FYFX  
       Jan 18, 2024
    > 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  
       Jan 18, 2024
    你把 v_Enable 放到外面初始化也是行的
    你这个 if(v_Enable := !v_Enable){...} 写法也挺魔法的。。。
    nexklee
        5
    nexklee  
    OP
       Jan 18, 2024
    @FYFX #3
    我那句是想当一个开关 按 XButton2 切换 一次非 一次是 是就按键 1 非就什么都不做 需要只执行一次吗 局部变量不行?
    nexklee
        6
    nexklee  
    OP
       Jan 18, 2024
    @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  
       Jan 18, 2024
    那里面可能要加一下 global v_Enable 声明,看你报错怎么说的
    nexklee
        8
    nexklee  
    OP
       Jan 18, 2024
    @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)
    {
    ...

    这种最奇葩的样子 成功
    About     Help     Advertise     Blog     API     FAQ     Solana     3179 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 13:38 PVG 21:38 LAX 06:38 JFK 09:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86