Jump to content

_IsPressed not woking in my loop


Recommended Posts

While -1
    While -1
        WinActivate($PID)
        Sleep(1000)
        Send("Hello")
        $timer = TimerInit()
        While -1
            PixelSearch($searchL,$searchT,$searchR,$searchB,"0x" & Hex($color,6),10,1)
--->        If _IsPressed("09",$dll) Then ExitLoop 3 ;HERE IS THE CODE THAT IS NOT WORKING
            If @error <> 1 Then ExitLoop
            If TimerDiff($timer) > 1000 Then
                Sleep(1000)
                WinActivate($PID)
                ExitLoop 2
            EndIf
        Wend
        $timer = TimerInit()
        MouseMove($shade[0], $shade[1])
        While -1
            PixelSearch($shade[0]-10,$shade[1]-10,$shade[0]+10,$shade[1]+10,"0x" & Hex($color,6),10,1)
--->        If _IsPressed("09",$dll) Then ExitLoop 3 ;HERE IS THE CODE THAT IS NOT WORKING
            If @error = 1 Then ExitLoop
            If TimerDiff($timer) > 5000 Then
                Sleep(1000)
                WinActivate($PID)
                Exitloop 2
            EndIf
         Wend
    Wend
 Wend
Setup()
EndFunc

I am having trouble with the above code. I am trying to have the loop bail and go to a Setup() Function if the TAB key is pressed. However when I press TAB nothing happens, it just continues the loop, please help!

Link to comment
Share on other sites

Here is the relevant documentation to help you resolve your issue.

https://www.autoitscript.com/autoit3/docs/keywords/While.htm (See Example 2, specifically read the code comments)

I also noticed you are missing the following line at the top of your script.

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

#Include <Misc.au3>

 

Also, consider using more functional programming. Each function should be in its own Func box. Refrain from putting all of your code in one function. This allows you to more easily reuse code instead of copying and pasting the same code in multiple locations.

 

Have a great day.

 

 

 

Link to comment
Share on other sites

I am not too certain which of example two is what I am looking for. If you are referring to using HotKeySet what might I do in order to accomplish ending the loop? If I just use: 

HotKeySet("{TAB}","setup")

Func Setup()
    ;whatever
EndFunc

This will correctly begin that function but it sort of "freezes"? as it continues the previous loop still and will not continue the new loop.

Link to comment
Share on other sites

Quote

; Create an endless loop, 1 will always be 1 therefore True.

While 1

You are using while -1, which will never happen without an expression to make the statement true.

While <expression>

The script will evaluate the expression and if it is true, it will execute the contents inside the while wend loop. Since 1 is functionally the same as true, while 1 will always execute code inside indefinitely until exitloop is performed.

While ProcessExist("Explorer.exe") > 0
    ConsoleWrite("I will write to console if the process 'Explorer.exe' exists. If not, the contents of this while wend loop will be ignored.")
wend

In this example, we are using ProcessExist as our expression. If you look at the ProcessExist documentation (https://www.autoitscript.com/autoit3/docs/functions/ProcessExists.htm), you will see this function returns a 0 if the process does not exist, and the pid if it does exist, which is any number above 0.  You could also use

While ProcessExist("Explorer.exe") <> 0

which means if ProcessExist is not 0 then do something. check out the operators page. https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm

This also means, if the process "Explorer.exe" does not exist, the while's expression will not be true, and the content inside the loop will stop executing and operation will continue just under the wend.

Edited by BetaLeaf
clarification

 

 

Link to comment
Share on other sites

Sorry, I am still not able to problem solve this alone. I now understand the expression used for While more clearly, I have changed over to always be true as I want it to always perform the loops (until I call upon ExitLoop)

For testing purpose I used

If _IsPressed("09", $dll) Then Exit

It is still seemingly completely ignoring this line as when I press TAB, hold TAB, whatever, it will just continue through the regular loop and never exit.

Also, is where I chose to insert the ExitLoop command the issue? Perhaps I can put it under the original While?

Edited by kjpolker
Link to comment
Share on other sites

That appears to have solved the issue with it not picking up hitting TAB. Also I had to move the function call at the end up above the last WEnd. Not sure why but ExitLoop 3 was not working properly, ExitLoop 2 seems fine.

This leaves me with a few more kinks I will have to research, as the called function no longer works properly coming out of the ExitLoop, perhaps it is a memory issue? Do I need to "clear cache" so to speak?

Edited by kjpolker
Link to comment
Share on other sites

Shouldn't need to, but if you feel something is buggy, give your computer a reboot and see if that helps. Most of the time, its a logic error. Be sure to check each functions help page and see what each function returns and make sure your logic is correct. For example, expecting a return 1 when it actually returns -1.

You can easily check each functions help page by double clicking on the function, then pressing F1.

Edited by BetaLeaf
Edit: Heading to bed for now. I'll ttyl tomorrow if you still need help.

 

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...