Jump to content

Recommended Posts

Posted

Hi all,
I have written an Auto-clicker for the purpose of automating a few processes that are repetitive. I think it even works well, so I want to share it with you.
Features:

  • Click speed
  • Mouse coordinate tracking
  • Hotkeys for ease of use
  • Saving macros to .json file
  • Reading macros from a .json file

Requires:
- JSON-UDF  (https://autoit.de/thread/85435-json-udf/)

I use the JSON-UDF created by AspirinJunkie, for which I would like to thank him. You have to download the UDF from his post:
https://autoit.de/thread/85435-json-udf/

GUI made in Koda 1.7.30 - also thanks to the author.
http://www.koda.darkhost.ru

The code may not be very readable, but I think you will find everything you need in it. I wasted my time making it pretty :)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#include <GuiStatusBar.au3>


#include <JSON.au3>


; #INDEX# =======================================================================================================================
; Title .........: Auto-clicker
; Version .......: 1.20
; AutoIt Version : 4.4.6
; Language ......: english
; Description ...: An application for automating processes requiring a large number of repetitive mouse clicks.
;                   Allows writing to and reading from a .json file.
;                   Features:
;                   - Left/right mouse button
;                   - Click speed
;                   - Mouse coordinate tracking
;                   - Hotkeys for ease of use
;                   - Saving macros to .json file
;                   - Reading macros from a .json file
;                   
;                   Requires:
;                   - JSON-UDF  (https://autoit.de/thread/85435-json-udf/)
;
; Author(s) .....: Dziku
; Last changed ..: 02-07-2024
; Link ..........: 
; License .......: This work is free.
;                  You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2,
;                  as published by Sam Hocevar.
;                  See http://www.wtfpl.net/ for more details.
; ===============================================================================================================================



#Region ### START Koda GUI section ### Form=D:\PROGRAMOWANIE\AutoIt\AUTO-CLICKER\auto-clicker-2.kxf
Global $AUTO = GUICreate("AUTO - CLICKER", 611, 369, 336, 233, BitOR($WS_MAXIMIZEBOX,$WS_MINIMIZEBOX,$WS_SYSMENU,$WS_GROUP,$WS_TABSTOP), BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetIcon("icon.ico", -1)
Global $Group1 = GUICtrlCreateGroup("Start / Stop", 5, 195, 206, 76)
Global $start = GUICtrlCreateButton("START", 10, 235, 85, 26)
Global $stop = GUICtrlCreateButton("STOP", 105, 235, 85, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
Global $Label5 = GUICtrlCreateLabel("START/STOP: F4", 15, 215, 92, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $Group2 = GUICtrlCreateGroup("Add macro", 5, 135, 201, 56)
Global $Button2 = GUICtrlCreateButton("Left click (F2)", 10, 155, 90, 25)
Global $Button3 = GUICtrlCreateButton("Right click (F3)", 105, 155, 90, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $Group3 = GUICtrlCreateGroup("Mouse click", 5, 5, 200, 125)
Global $mx = GUICtrlCreateInput("0", 100, 20, 96, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
Global $my = GUICtrlCreateInput("0", 100, 42, 96, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
Global $Label1 = GUICtrlCreateLabel("Mouse X:", 15, 24, 49, 17)
Global $Label2 = GUICtrlCreateLabel("Mouse Y:", 15, 42, 49, 17)
Global $Label3 = GUICtrlCreateLabel("Lock/unlock coords:  F1", 15, 65, 121, 17)
Global $mouseSpeed = GUICtrlCreateSlider(15, 105, 170, 20)
GUICtrlSetLimit(-1, 100, 1)
GUICtrlSetData(-1, 1)
GUICtrlSetTip(-1, "Mouse speed. 1- fast, 100 - slow")
Global $Label4 = GUICtrlCreateLabel("Mouse speed (1-100):", 15, 85, 107, 17)
Global $mouseSpeedInput = GUICtrlCreateInput("1", 125, 82, 56, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $ListView1 = GUICtrlCreateListView("ID|Button|Mouse X|Mouse Y|Speed", 220, 10, 365, 225,  BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS,$WS_VSCROLL), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 65)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 60)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 80)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 65)
GUICtrlSetFont(-1, 9, 400, 0, "Arial Nova")
Global $buttonDelete = GUICtrlCreateButton("Delete (DEL)", 475, 240, 106, 31)
Global $macroCounter = GUICtrlCreateLabel("0/0", 430, 240, 40, 17)
Global $loop = GUICtrlCreateCheckbox("Loop (F6)", 220, 240, 63, 17)
GUICtrlSetTip(-1, "Enable loop")
Global $windowMenu = GUICtrlCreateMenu("&File")
Global $menuFileNew = GUICtrlCreateMenuItem("New", $windowMenu)
GUICtrlCreateMenuItem("", $windowMenu)
Global $menuFileOpen = GUICtrlCreateMenuItem("Open...", $windowMenu)
GUICtrlCreateMenuItem("", $windowMenu)
Global $menuFileSave = GUICtrlCreateMenuItem("Save", $windowMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
Global $menuFileSaveAs = GUICtrlCreateMenuItem("Save as...", $windowMenu)
GUICtrlCreateMenuItem("", $windowMenu)
Global $menuExit = GUICtrlCreateMenuItem("Exit", $windowMenu)
Global $windowMenuInfo = GUICtrlCreateMenu("&Info")
Global $menuInfo = GUICtrlCreateMenuItem("About", $windowMenuInfo)
Global $statusBar = _GUICtrlStatusBar_Create($AUTO)
Global $statusBar_PartsWidth[2] = [450, -1]
_GUICtrlStatusBar_SetParts($statusBar, $statusBar_PartsWidth)
_GUICtrlStatusBar_SetText($statusBar, "Info", 0)
_GUICtrlStatusBar_SetText($statusBar, "Author: Dziku", 1)
Global $loopCount = GUICtrlCreateInput("1000", 300, 240, 72, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
Global $Updown1 = GUICtrlCreateUpdown($loopCount)
GUICtrlSetLimit(-1, 1000, 10)
GUICtrlSetTip(-1, "1 - 1000")
Global $Label6 = GUICtrlCreateLabel("Loops left:", 220, 265, 53, 17)
Global $loopsLeft = GUICtrlCreateLabel("1000", 280, 265, 56, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $x = 0
Global $getCoordSwitch = 1
Global $dealy = 0
Global $mouseButton = 0
Global $macroDelay = 0
Global $macroButton = $MOUSE_CLICK_LEFT
Global $macros[9999]
Global $macroAct = 0
Global $macroCount = 0
Global $filePath = ""
Global $macroSpeed = 1
Global $loopCounterValue = 1
Global $lastSelected = 0


HotKeySet("{F1}", "getCoordsOnOff")
HotKeySet("{F2}", "macroMouseLeft")
HotKeySet("{F3}", "macroMouseRight")
HotKeySet("{F4}", "startStop")
HotKeySet("{F5}", "mouseMoveToPoint")
HotKeySet("{F6}", "loopSelect")
HotKeySet("{DEL}", "macroDelete")





While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $menuExit
            ExitLoop
        Case $AUTO
        Case $start
            start()
        Case $stop
            stop()
        Case $Button2
            macroMouseLeft()
        Case $Button3
            macroMouseRight()
        Case $buttonDelete
            macroDelete()
        Case $menuInfo
            about()
        Case $menuFileNew
            menuFileNew()
        Case $menuFileOpen
            menuFileOpen()
        Case $menuFileSave
            menuFileSave()
        Case $menuFileSaveAs
            menuFileSaveAs()
        Case $menuInfo
            menuInfo()
        Case $mouseSpeed
            mouseSpeedChange()
        Case $loopCount
            GUICtrlSetData($loopsLeft,GUICtrlRead($loopCount))
    EndSwitch
    

    updateMacroCounter()
        
        
   If $x = 1 Then
      mainFunc()
   EndIf
    
    If $getCoordSwitch = 1 Then
        getCoords()
    EndIf
WEnd

Func about()
    MsgBox(BitOR($MB_OK,$MB_TOPMOST), "About", "An application for automating processes requiring a large number of repetitive mouse clicks." & @CRLF & "Allows writing to and reading from a .json file." & @CRLF & @CRLF & "Version: 1.2" &@CRLF &"Author: Dziku", 10)
EndFunc

Func loopSelect()
    If _IsChecked($loop) Then
            GUICtrlSetState($loop, $GUI_UNCHECKED )
    Else
        GUICtrlSetState($loop, $GUI_CHECKED )
    EndIf
EndFunc


Func macroDelete()
    _GUICtrlListView_DeleteItem ($ListView1, $macroAct)
    $macroCount = _GUICtrlListView_GetItemCount($ListView1)
EndFunc

Func macroChange()
    $actMacro = GUICtrlGetState($ListView1)
EndFunc

Func macroMouseLeft()
    $mouseButton = 0
    macroAdd()
EndFunc

Func macroMouseRight()
    $mouseButton = 1
    macroAdd()
EndFunc

Func macroAdd()
    If $mouseButton = 0 Then
        $macroButton = "LEFT"
    Else
        $macroButton = "RIGHT"
    EndIf
    $macroSpeed =  GUICtrlRead($mouseSpeed)
    $macroX = GUICtrlRead($mx)
    $macroY = GUICtrlRead($my)
    $thisMacro = $macroCount+1 & "|" & $macroButton & "|" & $macroX & "|" & $macroY & "|" & $macroSpeed
    $macroCount = $macroCount + 1
    $macros[$macroCount] = GUICtrlCreateListViewItem($thisMacro, $ListView1)
    $macroAct = _GUICtrlListView_GetItemCount($ListView1)
    _GUICtrlListView_SetItemChecked ( $ListView1, $macroAct-1 )
    _GUICtrlListView_ClickItem ($ListView1, $macroAct-1)
    updateMacroCounter()
EndFunc


Func updateMacroCounter()
    $macroCount = _GUICtrlListView_GetItemCount($ListView1)
    $macroAct = _GUICtrlListView_GetSelectionMark($ListView1)
    GUICtrlSetData($macroCounter,$macroAct+1 & "/" & $macroCount)
EndFunc


Func startStop()
    If $x = 0 Then
        start()
    Else
        stop()
    EndIf
EndFunc

Func start()
    $x = 1
    GUICtrlSetState($start, $GUI_DISABLE)
    GUICtrlSetState($stop, $GUI_ENABLE)
    GUICtrlSetState($loopCount,$GUI_DISABLE)
    $loopCounterValue = GUICtrlRead($loopCount)
    GUICtrlSetData($loopsLeft,$loopCounterValue)
    $lastSelected = _GUICtrlListView_GetSelectionMark($ListView1)
EndFunc

Func stop()
    $x = 0
    GUICtrlSetState($loopCount,$GUI_ENABLE)
    GUICtrlSetState($stop, $GUI_DISABLE)
    GUICtrlSetState($start, $GUI_ENABLE)
    $loopCounterValue = GUICtrlRead($loopCount)
    GUICtrlSetData($loopsLeft,$loopCounterValue)
    _GUICtrlListView_ClickItem ($ListView1, $lastSelected)
EndFunc

Func getCoordsOnOff()
    If $getCoordSwitch = 0 Then
        $getCoordSwitch = 1
    Else
        $getCoordSwitch = 0
    EndIf
EndFunc

func getCoords()
   $mPos = MouseGetPos()
   GUICtrlSetData($mx, $mPos[0])
   GUICtrlSetData($my, $mPos[1])
EndFunc


Func mouseSpeedChange()
    $macroSpeed =  GUICtrlRead($mouseSpeed)
    GUICtrlSetData($mouseSpeedInput,$macroSpeed)
EndFunc

Func mainFunc()
    $isChecked = _GUICtrlListView_GetItemChecked($ListView1, $macroAct)
        If $isChecked = True Then
            $aItem = _GUICtrlListView_GetItemTextArray($ListView1, $macroAct)
            $thisButton = $aItem[2]
            $clickX =  $aItem[3]
            $clickY =  $aItem[4]
            $delay = $aItem[5]
            If $thisButton = "LEFT" Then
                MouseMove ($clickX, $clickY, $aItem[5])
                MouseClick($MOUSE_CLICK_LEFT, $clickX, $clickY, 1)
            ElseIf $thisButton = "RIGHT" Then
                MouseMove ($clickX, $clickY, $macroSpeed)
                MouseClick($MOUSE_CLICK_RIGHT, $clickX, $clickY, 1)
            EndIf
        EndIf
            If $macroCount == $macroAct +1 Then
                ConsoleWrite("Count: "&$macroCount&"/"&$macroAct+1&@CRLF)
                If _IsChecked($loop) Then
                    If $loopCounterValue > 0 Then
                        $macroAct = 0
                        _GUICtrlListView_ClickItem ($ListView1, $macroAct)
                        $loopCounterValue = $loopCounterValue - 1
                        GUICtrlSetData($loopsLeft,$loopCounterValue)
                    Else
                        stop()
                    EndIf
                Else
                    startStop()
                EndIf
            Else
                $macroAct = $macroAct + 1
                _GUICtrlListView_ClickItem ($ListView1, $macroAct)
            EndIf
    updateMacroCounter()
EndFunc



Func _IsChecked($idControlID)
        Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc



Func loadFromFile($path)
    $thisData = FileRead ($filePath )
    $o_Object = _JSON_Parse($thisData)
            _GUICtrlListView_DeleteAllItems($ListView1)
    For $thisI = 0 To 9999 Step +1
            $thisX =  _JSON_Get($o_Object, "[" & $thisI & "].1") & "|" &  _JSON_Get($o_Object, "[" & $thisI & "].2") & "|" &  _JSON_Get($o_Object, "[" & $thisI & "].3") & "|" &  _JSON_Get($o_Object, "[" & $thisI & "].4") & "|" &  _JSON_Get($o_Object, "[" & $thisI & "].5")
            If _JSON_Get($o_Object, "[" & $thisI & "].0") = "True" Or _JSON_Get($o_Object, "[" & $thisI & "].0") = "False" Then
                $macros[$thisI] = GUICtrlCreateListViewItem($thisX, $ListView1)
                If _JSON_Get($o_Object, "[" & $thisI & "].0") = "True" Then
                    _GUICtrlListView_SetItemChecked ( $ListView1, $thisI )
                EndIf
                $macroAct = $thisI
                $macroCount = $thisI
            EndIf
    Next
EndFunc
            


Func menuFileNew()
        If MsgBox($MB_YESNO, "New file", "Do you want to save the changes to the file?", 10) = 6 Then
            menuFileSaveAs()
        Else
            GUISetState  ( @SW_MINIMIZE,$AUTO)
            _GUICtrlListView_DeleteAllItems($ListView1)
            Local Const $sMessage = "New file..."
            
            Local $sFileSaveDialog = FileSaveDialog($sMessage, @ScriptDir, "(*.json)", $FD_PATHMUSTEXIST)
            If @error Then
                    _GUICtrlStatusBar_SetText($statusBar, "File creation error", 0)
            Else
                    Local $sFileName = StringTrimLeft($sFileSaveDialog, StringInStr($sFileSaveDialog, "\", $STR_NOCASESENSEBASIC, -1))
                    WinSetTitle($AUTO, "", "AUTO-CLICKER - " & $sFileSaveDialog)
                    GUICtrlSetState($menuFileSave, $GUI_ENABLE)
                    $filePath = $sFileSaveDialog
                    _GUICtrlStatusBar_SetText($statusBar, "File created: " & $filePath, 0)
                    $filePath = $sFileName
                EndIf
                GUISetState  ( @SW_RESTORE  ,$AUTO)
        EndIf
EndFunc




Func menuFileOpen()
    GUISetState  ( @SW_MINIMIZE,$AUTO)
    $sFile = FileOpenDialog("Select file...", @ScriptDir, "(*.json)")
    If @error <> 1 Then
        WinSetTitle($AUTO, "", "AUTO-CLICKER - " & $sFile)
        GUICtrlSetState($menuFileSave, $GUI_ENABLE)
        _GUICtrlStatusBar_SetText($statusBar, "File open: " & $sFile, 0)
        $filePath = $sFile
        GUISetState  ( @SW_RESTORE  ,$AUTO)
        loadFromFile($filePath)
    Else
            _GUICtrlStatusBar_SetText($statusBar, "Error opening file", 0)
    EndIf
EndFunc



Func menuFileSave()
    GUISetState  ( @SW_MINIMIZE,$AUTO)
    If $filePath = "" Then
        menuFileSaveAs()
    Else
        Local $hFileOpen = FileOpen($filePath, $FO_OVERWRITE)
        FileFlush($hFileOpen)
        Local $sText = '['
        For $i = 0 To $macroCount-1 Step +1
            $aItem = _GUICtrlListView_GetItemTextArray($ListView1, $i)
            $sText &= '{'
            For $ii = 0 To $aItem[0]
                    If $ii = 0 Then
                        $aItem[$ii] = _GUICtrlListView_GetItemChecked($ListView1, $i)
                    EndIf
                    $sText &= '"' & $ii & '": "' & $aItem[$ii] & '",'
                Next
            $sText = StringTrimRight($sText,1)
            $sText &= '},'
        Next
        $sText = StringTrimRight ($sText,1)
        $sText &= ']'
        FileWrite($filePath,$sText)
        ConsoleWrite($sText)
        GUISetState  ( @SW_RESTORE  ,$AUTO)
        _GUICtrlStatusBar_SetText($statusBar, "File save: " & $filePath, 0)
    EndIf
EndFunc






Func menuFileSaveAs()
    GUISetState  ( @SW_MINIMIZE,$AUTO)
    $sFile = FileSaveDialog("Save file as...", @ScriptDir, "(*.json)", $FD_PATHMUSTEXIST)
    $filePath = $sFile
    WinSetTitle($AUTO, "", "AUTO-CLICKER - " & $filePath)
    Local $hFileOpen = FileOpen($sFile, $FO_OVERWRITE)
    FileFlush($hFileOpen)
    Local $sText = '['
    For $i = 0 To $macroCount-1 Step +1
        $aItem = _GUICtrlListView_GetItemTextArray($ListView1, $i)
        $sText &= '{'
        For $ii = 0 To $aItem[0]
                If $ii = 0 Then
                    $aItem[$ii] = _GUICtrlListView_GetItemChecked($ListView1, $i)
                EndIf
                $sText &= '"' & $ii & '": "' & $aItem[$ii] & '",'
            Next
        $sText = StringTrimRight($sText,1)
        $sText &= '},'
    Next
    $sText = StringTrimRight ($sText,1)
    $sText &= ']'
    FileWrite($filePath,$sText)
    ConsoleWrite($sText)
    GUICtrlSetState($menuFileSave, $GUI_ENABLE)
    GUISetState  ( @SW_RESTORE  ,$AUTO)
    _GUICtrlStatusBar_SetText($statusBar, "File saved: " & $filePath, 0)
EndFunc

 

Auto-clicker-v1.2.au3

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...