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

112 天前
 nexklee

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 摸着石头过河 很多不懂

794 次点击
所在节点    程序员
8 条回复
nexklee
112 天前
哭了 这排版都是啥呀 哪里还有什么第 9 行 /捂脸
nexklee
112 天前
FYFX
112 天前
> 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
112 天前
你把 v_Enable 放到外面初始化也是行的
你这个 if(v_Enable := !v_Enable){...} 写法也挺魔法的。。。
nexklee
112 天前
@FYFX #3
我那句是想当一个开关 按 XButton2 切换 一次非 一次是 是就按键 1 非就什么都不做 需要只执行一次吗 局部变量不行?
nexklee
112 天前
@FYFX #4

v_Enable := False


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

这是我最早的 但 V2 版报错
FYFX
112 天前
那里面可能要加一下 global v_Enable 声明,看你报错怎么说的
nexklee
112 天前
@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)
{
...

这种最奇葩的样子 成功

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1009570

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX