Jump to content
Christofer_Minestar

Way to close Ai3 Script with a button?

Recommended Posts

Christofer_Minestar

Hi, new here.

So I have tried WinClose, WinKill and ProcessClose with no avail, with ACTIVE, TITLE and CLASS, but can't get it to work.

Also, the red X button does not work, to end script I need to kill it in Task Manager, and that's what the button is supposed to do.

Code:

#cs ----------------------------------------------------------------------------
    Info: This is meant to repeat a set of keystrokes every {set} millisecond. {TAB} and others listed here: https://www.autohotkey.com/docs/Hotkeys.htm are supported
    AutoIt Version: 3.3.0.0
    Discord: @Christofer // BeyondProjects#6955
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

GUICreate("KRv4", 335, 75)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel("Key(s)", 20, 10)
$key1 = GUICtrlCreateInput("", 80, 8, 160)
GUICtrlCreateLabel("Time (ms)", 20, 44)
$time1 = GUICtrlCreateInput("", 80, 40, 160)

$startbutton = GUICtrlCreateButton("Start", 250, 7, 60)
$endbutton = GUICtrlCreateButton("End", 250, 39, 60)

While 1
    $msg = GUIGetMsg()

    Select

         Case $msg = $startbutton

                $send1 = GUICtrlRead($key1)
                $sleep1 = GUICtrlRead($time1)

            While 1
                Send($send1)
                Sleep($sleep1)
            WEnd


         Case $msg = $endbutton

                WinClose("[ACTIVE]", "")

    EndSelect

WEnd
$timer = TimerInit()

 

KRv4.au3

Share this post


Link to post
Share on other sites
Christofer_Minestar

Forgot to say: The button that I'm trying to get to close the script is

Quote

$endbutton

 

Share this post


Link to post
Share on other sites
JLogan3o13

@Christofer_Minestar welcome to the forum.

 First, to close the script, for $endButton, just ExitLoop.

Case $msg = $endbutton
            ExitLoop

Second, this will work just fine until you hit the other button. As soon as you hit $startbutton you are creating an infinite While loop.

While 1
                Send($send1)
                Sleep($sleep1)
            WEnd

Since this loop never ends, your click of the $endbutton is obviously never going to be recognized.

Lastly, it appears that you are porting this script from AHK. Please ensure you read our forum rules before continuing; unlike that forum we do not tolerate any game automation scripts (which a lot of 'click random buttons x number of times' scripts turn out to be for). Do not be surprised if, in the course of assisting you with your script, you aren't asked to explain why you need to send so many button presses, as there is almost always a better way to accomplish a legitimate script.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Christofer_Minestar
19 hours ago, JLogan3o13 said:

First, to close the script, for $endButton, just ExitLoop.

Case $msg = $endbutton
            ExitLoop

Second, this will work just fine until you hit the other button. As soon as you hit $startbutton you are creating an infinite While loop.

While 1
                Send($send1)
                Sleep($sleep1)
            WEnd

 

Thanks! I'll try to find a better way to do this.

Quote

Lastly, it appears that you are porting this script from AHK.

I am not porting this from AKH, the link I put there is just because it supports AHK-like modifiers and keys

Share this post


Link to post
Share on other sites
JLogan3o13
2 minutes ago, Christofer_Minestar said:

I am not porting this from AKH, the link I put there is just because it supports AHK-like modifiers and keys

Glad to hear. The friendly reminder about the forum rules still applies. Aside from that, hope you are successful in what you're trying to do :)

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Christofer_Minestar

Some progress:

End button still works after using start button, but actual function of the key repeat is gone now, ah well. Keep trying I guess. ^_^
New code:

#cs ----------------------------------------------------------------------------
    Key Repeat version 4
    Info: This is meant to repeat a set of keystrokes every {set} millisecond.
    {TAB} and others are supported, listed here: https://www.autohotkey.com/docs/Hotkeys.htm
    AutoIt Version: 3.3.0.0
    Discord: @Christofer // BeyondProjects#6955
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

GUICreate("Key Repeat 4", 335, 75)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel("Key(s)", 20, 10)
$key1 = GUICtrlCreateInput("", 80, 8, 160)
GUICtrlCreateLabel("Time (ms)", 20, 44)
$time1 = GUICtrlCreateInput("", 80, 40, 160)

$startbutton = GUICtrlCreateButton("Start", 250, 7, 60)
$endbutton = GUICtrlCreateButton("End", 250, 39, 60)

While 1
    $msg = GUIGetMsg()

    Select

         Case $msg = $startbutton

                $send1 = GUICtrlRead($key1)
                $sleep1 = GUICtrlRead($time1)

            While 1
                Send($send1)
                Sleep($sleep1)
                ExitLoop
            WEnd


         Case $msg = $endbutton
               ExitLoop
                WinClose("[ACTIVE]", "")

    EndSelect

WEnd
$timer = TimerInit()

 

Share this post


Link to post
Share on other sites
LarsJ

With the code in first post you can do it this way:

#cs ----------------------------------------------------------------------------
  Info: This is meant to repeat a set of keystrokes every {set} millisecond. {TAB} and others listed here: https://www.autohotkey.com/docs/Hotkeys.htm are supported
  AutoIt Version: 3.3.0.0
  Discord: @Christofer // BeyondProjects#6955
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <MenuConstants.au3>

$hGui = GUICreate("KRv4", 335, 75)

GUIRegisterMsg( $WM_COMMAND, "WM_COMMAND" )
GUIRegisterMsg( $WM_SYSCOMMAND, "WM_SYSCOMMAND" )

GUISetState(@SW_SHOW)

GUICtrlCreateLabel("Key(s)", 20, 10)
$key1 = GUICtrlCreateInput("", 80, 8, 160)
GUICtrlCreateLabel("Time (ms)", 20, 44)
$time1 = GUICtrlCreateInput("", 80, 40, 160)

$startbutton = GUICtrlCreateButton("Start", 250, 7, 60)
$endbutton = GUICtrlCreateButton("End", 250, 39, 60)

While 1
  Switch GUIGetMsg()
   Case $startbutton
    $send1 = GUICtrlRead($key1)
    $sleep1 = GUICtrlRead($time1)
    While 1
      Send($send1)
      Sleep($sleep1)
    WEnd
  EndSwitch
WEnd

Func WM_COMMAND( $hWnd, $iMsg, $wParam, $lParam )
  Switch BitAND( $wParam, 0xFFFF ) ; LoWord
    Case $endbutton
      Switch BitShift( $wParam, 16 ) ; HiWord
        Case $BN_CLICKED
          Exit
      EndSwitch
  EndSwitch
EndFunc

Func WM_SYSCOMMAND( $hWnd, $iMsg, $wParam, $lParam )
  Switch $hWnd
    Case $hGui
      Switch $wParam
        Case $SC_CLOSE
          Exit
      EndSwitch
  EndSwitch
EndFunc

The Autoit message loop is blocked, but not Windows.

Share this post


Link to post
Share on other sites
Christofer_Minestar
6 hours ago, LarsJ said:

With the code in first post you can do it this way:

The Autoit message loop is blocked, but not Windows.

Wow, thanks! That's much cleaner. The reason I'm doing this is because my friend challenged me to revive and improve one of my old projects, and back when i made this I had very little AutoIt experience.

I don't have time now but I will try the code later, thanks!

Share this post


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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.