Jump to content

Do Untill OR


 Share

Recommended Posts

Hi,

Hoping someone can help me on the logic, I just can't seem to find a way to exit the "when checked" loop using the exit button. Thanks before hand.

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.6.1
    Author:

    Script Function:
    Move Mouse

#ce ----------------------------------------------------------------------------

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("MouseMove", 200, 69, 310, 296)
WinSetOnTop("MouseMove", "", 1)
$Checkbox1 = GUICtrlCreateCheckbox(" ON or OFF", 8, 16, 89, 33)
$Button1 = GUICtrlCreateButton("Exit", 104, 24, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW, $Form1)
While 1
    $nMsg2 = GUIGetMsg()
    Select
        Case $nMsg2 = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg2 = $Checkbox1
            $check_state_of_checkbox = GUICtrlRead($Checkbox1)
            If $check_state_of_checkbox = $GUI_CHECKED Then
                Do
                    $pos = MouseGetPos(0)
                    $pos1 = MouseGetPos(1)
                    $pos2 = $pos + 2
                    $pos3 = $pos1 + 2
                    MouseMove($pos2, $pos3)
                    Sleep(1000)
                    $pos4 = MouseGetPos(0)
                    $pos5 = MouseGetPos(1)
                    $pos6 = $pos4 - 2
                    $pos7 = $pos5 - 2
                    MouseMove($pos6, $pos7)
                    Sleep(1000)
                    $check_state_of_checkbox = GUICtrlRead($Checkbox1)
                    $check_state_of_Button1 = GUICtrlRead($Button1)
                Until $check_state_of_checkbox = $GUI_UNCHECKED ;or IF $check_state_of_Button1 = 4 then exit
            EndIf
        Case $nMsg2 = $Button1
            GUIDelete($Form1)
            ExitLoop
    EndSelect
WEnd

;MsgBox(64,"","" & $Button1)
Link to comment
Share on other sites

The problem is that when you click a checkbox, no message is sent to the window telling you that the user did that. You can solve it by checking the checkbox all the time like this:

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.6.1
    Author:

    Script Function:
    Move Mouse

#ce ----------------------------------------------------------------------------

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("MouseMove", 200, 69, 310, 296)
WinSetOnTop("MouseMove", "", 1)
$Checkbox1 = GUICtrlCreateCheckbox(" ON or OFF", 8, 16, 89, 33)
$Button1 = GUICtrlCreateButton("Exit", 104, 24, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW, $Form1)
While 1
    $nMsg2 = GUIGetMsg()
    Select
        Case $nMsg2 = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg2 = $Checkbox1
            ; THIS NEVER HAPPENS
        Case $nMsg2 = $Button1
            GUIDelete($Form1)
            ExitLoop
    EndSelect

    $check_state_of_checkbox = GUICtrlRead($Checkbox1)
    If $check_state_of_checkbox = $GUI_CHECKED Then
        Do
            $pos = MouseGetPos(0)
            $pos1 = MouseGetPos(1)
            $pos2 = $pos + 2
            $pos3 = $pos1 + 2
            MouseMove($pos2, $pos3)
            Sleep(1000)
            $pos4 = MouseGetPos(0)
            $pos5 = MouseGetPos(1)
            $pos6 = $pos4 - 2
            $pos7 = $pos5 - 2
            MouseMove($pos6, $pos7)
            Sleep(1000)
            $check_state_of_checkbox = GUICtrlRead($Checkbox1)
            $check_state_of_Button1 = GUICtrlRead($Button1)
        Until $check_state_of_checkbox = $GUI_UNCHECKED ;or IF $check_state_of_Button1 = 4 then exit
    EndIf
WEnd

;MsgBox(64,"","" & $Button1)

The above code works as you intended.

Link to comment
Share on other sites

Hi Manadar,

Thanks for your quick response, the code seems to work exactly the same as before. Maybe I am missing something, my problem is that the exit button is not being read/processed while the do ... untill loop is running. The below bold code is where I am having problems.

Until $check_state_of_checkbox = $GUI_UNCHECKED ;[b]or IF [/b]$check_state_of_Button1 = [b]4 then [/b]exit
Link to comment
Share on other sites

The problem is that with those long sleeps the change the click gets noticed is extremely small, unless you keep it down for a long time.

The easy way around this would involve using Opt("GUIOnEventMode",1), but if you want to stick with GUIGetMsg(), you need to get rid of the sleeps.

This can be done like this:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bRun = False, $bAdd2 = True, $iSec

$Form1 = GUICreate("MouseMove", 200, 69, 310, 296)
WinSetOnTop("MouseMove", "", 1)
$Checkbox1 = GUICtrlCreateCheckbox(" ON or OFF", 8, 16, 89, 33)
$Button1 = GUICtrlCreateButton("Exit", 104, 24, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW, $Form1)

While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Checkbox1
    If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                $bRun = True
            Else
                $bRun = False
            EndIf
    Case $Button1
    GUIDelete($Form1)
    ExitLoop
    EndSwitch
    If $bRun And @SEC <> $iSec Then
        If $bAdd2 Then
            MouseMove(MouseGetPos(0)+2, MouseGetPos(1)+2)
        Else
            MouseMove(MouseGetPos(0)-2, MouseGetPos(1)-2)
        EndIf
        $iSec = @SEC
        $bAdd2 = Not $bAdd2
    EndIf
WEnd
Link to comment
Share on other sites

And what about this version:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("MouseCoordMode", 2)

$Form1 = GUICreate("MouseMove", 200, 69, 310, 296)
$Checkbox1 = GUICtrlCreateCheckbox(" ON or OFF", 8, 16, 89, 33)
$Button1 = GUICtrlCreateButton("Exit", 104, 24, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW, $Form1)

$pos = ControlGetPos("", " ON or OFF", $Checkbox1)
$move = 1

Do
    If GUICtrlRead($Checkbox1) = $GUI_CHECKED And $move Then
        MouseMove($pos[0] + 5, $pos[1] + 14)
        $move = 0
    EndIf
    If GUICtrlRead($Checkbox1) = $GUI_UNCHECKED Then $move = 1

Until GUIGetMsg() = $Button1

GUIDelete($Form1)
Exit

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

If it helps, here is a version with comments:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bRun = False ;This variable determines if the moves should be executed (when True), or skipped (when False).
Global $bAdd2 = True ;This variable determines if two pixels should be added to the original x and y posisition (when True), or substracted (when False).
Global $iSec ;We'll use this to get the 1 second delay, without using Sleep().

;Didn't change your UI
$Form1 = GUICreate("MouseMove", 200, 69, 310, 296)
WinSetOnTop("MouseMove", "", 1)
$Checkbox1 = GUICtrlCreateCheckbox(" ON or OFF", 8, 16, 89, 33)
$Button1 = GUICtrlCreateButton("Exit", 104, 24, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW, $Form1)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
    Exit
        Case $Checkbox1
            ;Instead of executing the moves, this part of the script just sets the flag if it should run, or idle.
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
    $bRun = True
    Else
    $bRun = False
    EndIf
    Case $Button1
        GUIDelete($Form1)
        ExitLoop
    EndSwitch
    
    ;This part does the moving if:
    ;The flag "$bRun" is set to True (If $bRun Then...)
    ;The value of @SEC has changed from the lst time it ran (If @SEC <> $iSec). This replaces Sleep(1000).
    If $bRun And @SEC <> $iSec Then
    If $bAdd2 Then ;If $bAdd2 is True the mouse will move to the bottom right, else the top left
    MouseMove(MouseGetPos(0)+2, MouseGetPos(1)+2) ;By using MouseGetPos in-line you can lower the amount of variables used, which is nice on larger projects.
    Else
    MouseMove(MouseGetPos(0)-2, MouseGetPos(1)-2)
    EndIf
    $iSec = @SEC ;Store the current value of $iSec to avoid another mousemove for one second. (works together with line 35)
    $bAdd2 = Not $bAdd2 ;Flip the direction the mouse should move in next. (works together with line 36)
    EndIf
WEnd
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...