Jump to content

Change button text and set hotkey to button


 Share

Recommended Posts

I have an auto mouse clicker and I have 2 questions I would like to ask, so I can tweak it. My first question is how do I change the text in a button after it has been pressed? So I enter my data and click "Start" and it will start clicking, now since I clicked the button it now says "Stop" and when pressed, it stops the script. How would I write stop script and the button change?

My next question is how do I tie a button to a key when the window is active? So if I am clicked in the program and I hit a key ("ENTER" for example) it than means the same thing as pressing "start"?

And last but not least I made a button that I want to retrieve coordinates of the users choice by clicking on the screen, I guess I use MouseGetPos() but how do I make it so by "clicking" it locks the coordinates in place and writes them to input boxes?

Edited by kjpolker
Link to comment
Share on other sites

  • Moderators

kjpolker,

Here you go - I think the comments explain it all: :)

#include <GUIConstantsEx.au3>
#include <Misc.au3>

; Set ENTER to run the _Action function when pressed
HotKeySet("{ENTER}", "_Action")

; Create GUI
GUICreate("Test", 225, 100, 200, 200)

; Action button
$actionitem = GUICtrlCreateButton("Start", 10, 10, 80, 30)
; Mouse coord button
$mousecoord = GUICtrlCreateButton("Mouse", 10, 50, 80, 30)
; Mouse coord inputs
$hInput_X = GUICtrlCreateInput("", 150, 10, 30, 20)
$hInput_Y = GUICtrlCreateInput("", 150, 50, 30, 20)

GUISetState()

While 1

    Switch GUIGetMsg()
        ; [X] pressed
        Case $GUI_EVENT_CLOSE
            ExitLoop
        ; Action button pressed
        Case $actionitem
            _action()
        ; Mouse button pressed
        Case $mousecoord
            _get_mousepos()
    EndSwitch

WEnd

GUIDelete()
Exit

Func _Action()
    Switch GUICtrlRead($actionitem)
        ; If button reads "Start"
        Case "Start"
            GUICtrlSetData($actionitem, "Stop")  ; change button text
            ; whatever you need as start code here
            MsgBox(0, "", "Started") ; Just as an indication
        ; If button reads "Stop"
        Case "Stop"
            GUICtrlSetData($actionitem, "Start")  ; change button text
            ; whatever you need as stop code here
            MsgBox(0, "", "Stopped") ; Just as an indication
    EndSwitch
EndFunc   ;==>_Action

Func _get_mousepos()

    ; Wait until left mouse button pressed
    $dll = DllOpen("user32.dll")
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    DllClose($dll)

    ; Get mouse coords and place in inputs
    $aMouse_Pos = MouseGetPos()
    GUICtrlSetData($hInput_X, $aMouse_Pos[0])
    GUICtrlSetData($hInput_Y, $aMouse_Pos[1])

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

All seemed to go smoothly until I clicked the "get coords" button, here is the function of it:

Global $Mouse_Pos, $dll
Func _get_mousepos()
    $dll = DllOpen("user32.dll")
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    DllClose($dll)
    $Mouse_Pos = MouseGetPos()
    GUICtrlSetData($X_Input, $Mouse_Pos[0])
    GUICtrlSetData($Y_Input, $Mouse_Pos[1])
EndFunc

It tells me that _IsPressed unknown function name?

Edited by kjpolker
Link to comment
Share on other sites

Is the #Include <Misc.au3> line still in your source code?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

kjpolker,

I presume you have a Opt(MustDeclareVars, 1) line in there somewhere! It is a good practice to declare variables - saves a lot of heartache when debugging - but I tend not to for very small scripts. :) To remedy the problem, I would declare the $MousePos array and the $dll as Local variables, as they are only used in the _get_mousepos function:

Func _get_mousepos()
    Local $dll = DllOpen("user32.dll")
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    DllClose($dll)
    Local $Mouse_Pos = MouseGetPos()
    GUICtrlSetData($X_Input, $Mouse_Pos[0])
    GUICtrlSetData($Y_Input, $Mouse_Pos[1])
EndFunc

Look in the Help file to see the very important distinction between Global and Local variables.

whats the dll do and how would anyone possibly know to include that?

1. Makes the _IsPressed function work by using an existing Windows DLL do the hard work looking for key/button presses.

2. Perhaps because they looked at _IsPressed in the Help file? :P

If it says _IsPressed is unknown, check #Include <Misc.au3> is still in your code.

M23

P.S. If you keep editing your post and changing your question you will not get the answers you are looking for! :)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Well I assumed it was better than starting a new topic by leading off of another question, it's just when I ask a question and it gets answered it makes me want to tweak the coding more and more so I just keep asking questions =P Now I have another question but I guess I won't worry about it lol.

Link to comment
Share on other sites

  • Moderators

kjpolker,

Asking questions is fine, it is changing them while people are answering the first set that is a bit off. :)

I hope we answered all the questions last time. What was the new one?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Ha, I am just frustrated, the codes you guys give me are perfect, but it leads to other problems cause my noob scripting skills, here is my entire script so far:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>
#Include <Misc.au3>

FileInstall('G:\Programs\AMC\Icons\AMC.jpg', @ScriptDir & '\Icons\AMC.jpg')
FileInstall('G:\Programs\AMC\Icons\Mouse.ico', @ScriptDir & '\Icons\Mouse.ico')

#Region ### START Koda GUI section ### Form=
HotKeySet("{ENTER}", "_Action")
$Form1 = GUICreate("Auto Mouse Clicker", 287, 165, 192, 124)
GUISetIcon(@ScriptDir & '\Icons\Mouse.ico')
GUISetBkColor(0x000000)
$Pic1 = GUICtrlCreatePic(@ScriptDir & '\Icons\AMC.jpg', 16, 0, 257, 57, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Button1 = GUICtrlCreateButton("Start", 110, 128, 73, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Get Coords", 200, 128, 73, 25)
$Xinput = GUICtrlCreateInput("", 72, 56, 73, 21)
$Yinput = GUICtrlCreateInput("", 72, 88, 73, 21)
$Label1 = GUICtrlCreateLabel("X-Coord", 8, 56, 64, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label2 = GUICtrlCreateLabel("Y-Coord", 8, 88, 63, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Timeinput = GUICtrlCreateInput("30", 168, 56, 49, 21)
$Label3 = GUICtrlCreateLabel("Seconds", 219, 60, 66, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label4 = GUICtrlCreateLabel("How often to" & @LF & "click in seconds.", 160, 80, 200, 37)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$LeftRadio = GUICtrlCreateRadio("Radio1", 6, 120, 17, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$RightRadio = GUICtrlCreateRadio("Radio2", 6, 140, 17, 17)
$Label5 = GUICtrlCreateLabel("Left Mouse?", 22, 120, 87, 21)
GUICtrlSetFont(-1, 8, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label6 = GUICtrlCreateLabel("Right Mouse?", 22, 141, 85, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Auto Mouse Clicker is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc
Local $Xinput, $Yinput, $Input3, $hLabel, $Button2
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            Sleep(100)
            ToolTip('Please click anywhere on the screen to set your coordinates', 1024, 738)
            _get_mousepos()
        Case $Button1
            _Action()
            If GUICtrlRead($LeftRadio) = 1 Then
        $sMouse_Button = "left"
        Else
        $sMouse_Button = "right"
        EndIf
    $X_Pos = GUICtrlRead($Xinput)
    $Y_Pos = GUICtrlRead($Yinput)
    $T_ms =GUICtrlRead($Timeinput) * 1000
    While 1
        MouseClick($sMouse_Button, $X_Pos, $Y_Pos, 1, $T_ms)
        $Begin = TimerInit()
        While TimerDiff($Begin) < $T_ms
                    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
        WEnd
    WEnd

    EndSwitch

WEnd

Func _Action()
    Local $sMouse_Button, $X_Pos, $Y_Pos, $T_ms, $Begin
    Switch GUICtrlRead($Button1)
        Case "Start"
            GUICtrlSetData($Button1, "Stop")
        Case "Stop"
            GUICtrlSetData($Button1, "Start")
    EndSwitch
EndFunc

Func _get_mousepos()
    Local $X_Input, $Y_Input
    Local $dll = DllOpen("user32.dll")
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    DllClose($dll)
    Local $Mouse_Pos = MouseGetPos()
    GUICtrlSetData($X_Input, $Mouse_Pos[0])
    GUICtrlSetData($Y_Input, $Mouse_Pos[1])
EndFunc

The problem is that when I click start the program starts and the button changes to stop, but when I click stop it doesn't change or stop, this is obviously because I have nothing in the Case "Stop" which is because I don't know how to stop a script, I only know how to pause, or terminate. As well as the "Get Coord" button is not functioning properly so it is more than likely an error on my part of the script. There are no actual errors with it that come up but when I click the button and click on the screen, nothing appears to happen, the inputs don't change. :)

Link to comment
Share on other sites

  • Moderators

kjpolker,

Integration of several elements is always a tricky part of coding - the various bits often do not want to play nicely together! :)

There were a number of problems which I think I have resolved:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>
#include <Misc.au3>

FileInstall('G:\Programs\AMC\Icons\AMC.jpg', @ScriptDir & '\Icons\AMC.jpg')
FileInstall('G:\Programs\AMC\Icons\Mouse.ico', @ScriptDir & '\Icons\Mouse.ico')

HotKeySet("{ENTER}", "_Action")
HotKeySet("{PAUSE}", "TogglePause")

Global $Paused
;Local $Xinput, $Yinput, $Input3, $hLabel, $Button2  ; <<<<<<<<<<<<<<<<<<< Not needed - they are automatically Global in scope

$Form1 = GUICreate("Auto Mouse Clicker", 287, 165, 192, 124)
GUISetIcon(@ScriptDir & '\Icons\Mouse.ico')
GUISetBkColor(0x000000)
$Pic1 = GUICtrlCreatePic(@ScriptDir & '\Icons\AMC.jpg', 16, 0, 257, 57, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Button1 = GUICtrlCreateButton("Start", 110, 128, 73, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Get Coords", 200, 128, 73, 25)
$Xinput = GUICtrlCreateInput("", 72, 56, 73, 21)
$Yinput = GUICtrlCreateInput("", 72, 88, 73, 21)
$Label1 = GUICtrlCreateLabel("X-Coord", 8, 56, 64, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label2 = GUICtrlCreateLabel("Y-Coord", 8, 88, 63, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Timeinput = GUICtrlCreateInput("30", 168, 56, 49, 21)
$Label3 = GUICtrlCreateLabel("Seconds", 219, 60, 66, 21)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label4 = GUICtrlCreateLabel("How often to" & @LF & "click in seconds.", 160, 80, 200, 37)
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
GUIStartGroup() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<< This should be in to create a group of radio buttons
$LeftRadio = GUICtrlCreateRadio("Radio1", 6, 120, 17, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$RightRadio = GUICtrlCreateRadio("Radio2", 6, 140, 17, 17)
GUIStartGroup() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<< This should be in to end the group of radio buttons
$Label5 = GUICtrlCreateLabel("Left Mouse?", 22, 120, 87, 21)
GUICtrlSetFont(-1, 8, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
$Label6 = GUICtrlCreateLabel("Right Mouse?", 22, 141, 85, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Lucida Calligraphy")
GUICtrlSetColor(-1, 0x00FF00)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            ToolTip('Please click anywhere on the screen to set your coordinates', 1024, 738)
            _get_mousepos()
            ToolTip("")
        Case $Button1
            _Action()
    EndSwitch

WEnd

Func TogglePause()
    If GUICtrlRead($Button1) = "Start" Then Return ; <<<<<<<<<<<<<<<<<< No point in pausing if it is not auto-clicking!
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        ToolTip('Auto Mouse Clicker is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause

Func _Action()
    Local $sMouse_Button, $X_Pos, $Y_Pos, $T_ms, $Begin
    GUICtrlSetData($Button1, "Stop")
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is where you "do things"
    If GUICtrlRead($LeftRadio) = 1 Then
        $sMouse_Button = "left"
    Else
        $sMouse_Button = "right"
    EndIf
    $X_Pos = GUICtrlRead($Xinput)
    $Y_Pos = GUICtrlRead($Yinput)
    $T_ms = GUICtrlRead($Timeinput) * 1000
    While 1
        MouseClick($sMouse_Button, $X_Pos, $Y_Pos, 1) ; $T_ms) <<<<<<<<< Speed here is speed of mouse movement to the position, not delay
        $Begin = TimerInit() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This section gives the delay
        While TimerDiff($Begin) < $T_ms
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $Button1
                    GUICtrlSetData($Button1, "Start")
                    ExitLoop 2
            EndSwitch
        WEnd
    WEnd
EndFunc   ;==>_Action

Func _get_mousepos()
    If GUICtrlRead($Button1) = "Stop" Then Return ; <<<<<<<<<<<<<<<< Only works when mouse if not auto-clicking
    ;Local $X_Input, $Y_Input 
    #cs
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Not the same spelling as when created in GUI!!!!!!!!!!!!!!
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The originals are Global.  Redeclaring them as Local would override this. 
    #ce
    Local $dll = DllOpen("user32.dll")
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    DllClose($dll)
    Local $Mouse_Pos = MouseGetPos()
    GUICtrlSetData($Xinput, $Mouse_Pos[0]) ; <<<<<<< Changed spelling to match input created in GUI
    GUICtrlSetData($Yinput, $Mouse_Pos[1]) ; <<<<<<< Changed spelling to match input created in GUI
EndFunc   ;==>_get_mousepos

A few points:

1. You need to learn about Global/Local variables - it is really important.

2. You must check the spelling of your variables - $X_Input is not the same as $XInput.

3. Try and structure your code a little better, perhaps as I have:

Includes

Directives

Declarations

Main code

Functions

I have marked the salient points in the script. I hope everything works now - do ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It's perfect, thank you :) And thanks for tips as well, I guess my real problem is completely understanding every little thing in Autoit which is why I am doing multiple programs at once, and each program is totally different than the other so I can get a feel of a variety of things. I guess learning this stuff just comes with practice, cause I still have trouble knowing what to #include. But thanks again :)

Edited by kjpolker
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...