这两个代码示例之间有什么区别?

huangapple go评论47阅读模式
英文:

What is the difference between these two examples of code?

问题

以下是您要翻译的内容:

我已经在过去的一年中在各处都在使用While GetKeyState("<some key>", "p"),而没有真正思考实际发生了什么。

space::
While GetKeyState("space", "p")
	{
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
	 Sleep 500
	}

上面的Tooltip命令是否只触发每半秒一次?
如果是这样,那么以下代码是如何工作的?因为没有Sleep 500

space::
if GetKeyState("space", "p")
	{
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
	}

我认为它应该只触发一次。要再次触发,我必须释放并再次按下Space键,但事实并非如此。Tooltip命令会持续触发,就像在While示例中一样。

感谢任何帮助。

英文:

I have been using While GetKeyState("<some key>", "p") everywhere for best part of a year without really thinking about what is actually happening.

space::
While GetKeyState("space", "p")
	{
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
	 Sleep 500
	}

Is the above Tooltip command, essentially only triggering every half a second?
if so then how is the following working? Since Sleep 500 is absent:

space::
if GetKeyState("space", "p")
	{
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
	}

I am thinking it should only fire once. To fire again I have to release and press Space again, but that is not the case. The Tooltip command continuously fires just like in the While example.

Thanks for any help.

答案1

得分: 1

第一个提示命令每半秒触发一次,因为有 Sleep 500

第二个命令在按住键时持续触发:

space::
	; if GetKeyState("space", "p") ; 多余的
		; {
		 MyVar++
		 Tooltip, % "MyVarIs: " MyVar
		; }
return

要使其仅在释放键时触发一次,请使用 Up 变体:

space Up::
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
return

或者使用 KeyWait 命令。

英文:

The first Tooltip command is triggering every half a second because of Sleep 500.

The second is continuously triggering if the key is holding down:

space::
; if GetKeyState("space", "p") ; redundant
	; {
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
	; }
return

To make it fire only once (by releasing), use the Up variant:

space Up::
	 MyVar++
	 Tooltip, % "MyVarIs: " MyVar
return 

or the KeyWait command.

huangapple
  • 本文由 发表于 2023年5月25日 00:28:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定