Jump to content

While Loop: Prevent Things From Infinitely Repeating


Recommended Posts

Hi guys, I want to ask you a question regarding the while loops. How can I prevent an action from repeating when it is in a while loop: Take a look at the script below:

#include <misc.au3>

global $body = GUICreate("Simple Window",430,340)
GUISetState()

while 1
    switch GUIGetMsg()
    case -3
        exitloop
    endswitch
    
    if _ispressed("0d") then
        GUICtrlCreateLabel("Some Text Here",10,10,120,20)
    endif ;it keeps running on and on
    
    ; ...
    
wend

How can I prevent the script from repeatedly creating the label when I press the button.

Thanks in advance!

Link to post
Share on other sites

Try this :

#include <misc.au3>

Global $body = GUICreate("Simple Window", 430, 340)
Local $idDum = GUICtrlCreateDummy()
Local $aKey[1][2] = [["{ENTER}",$idDum]]
GUISetAccelerators($aKey)
GUISetState()

While 1
  Switch GUIGetMsg()
    Case -3
      ExitLoop
    Case $idDum
      GUICtrlCreateLabel("Some Text Here", 10, 10, 120, 20)
      GUISetAccelerators("")
      ConsoleWrite ("check if only once" & @CRLF)
  EndSwitch

WEnd

 

Link to post
Share on other sites

The helpfile mentions this specifically....

Quote

_IsPressed() will return 1 until the key is released.
Even brief key presses can result in multiple returns within a loop.
If the code called does not include a blocking function (such as MsgBox) and the user does not require multiple returns, the script should wait until _IsPressed() returns 0 before continuing.

https://www.autoitscript.com/autoit3/docs/libfunctions/_IsPressed.htm

#include <misc.au3>
#include <GUIConstantsEx.au3>

global $body = GUICreate("Simple Window",430,340)
GUISetState()

while 1
    switch GUIGetMsg()
    case $GUI_EVENT_CLOSE
        exitloop
    endswitch

    if _ispressed("0d") then ;Enter Key
        while _ispressed("0d") <> 0
            ; wait till released
        Wend
        GUICtrlCreateLabel("Some Text Here",10,10,120,20)
        ConsoleWrite ("check if only once" & @CRLF)
    endif ;it keeps running on and on

    ; ...

wend

 

Edited by Bilgus
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...