Tyrus Posted February 27 Share Posted February 27 Good day all, I've been trying to learn AutoIt for a while now, and I'm just struggling, when I click the button here nothing happens? expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; Create the main GUI window Global $hGUI = GUICreate("Color Clicker", 300, 150) ; Create a label for the hex color code text field GUICtrlCreateLabel("Enter a hex color code (e.g. FFFFFF):", 10, 10, 280, 20) ; Create a text field for the hex color code Global $hTextField = GUICtrlCreateInput("", 10, 30, 280, 20) ; Create a button to start the color clicker Local $hButton = GUICtrlCreateButton("Click Color", 10, 60, 80, 30) GUICtrlSetOnEvent(-1, "ButtonClick") ; Show the GUI window GUISetState(@SW_SHOW) ; GUI event loop While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; Button click event handler Func ButtonClick() ; Get the hex color code from the text field Local $sHexCode = GUICtrlRead($hTextField) ; Convert the hex color code to a decimal color value Local $iColor = Hex($sHexCode, 6) ; Search the screen for pixels of the specified color and click them Local $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1) While IsArray($aPos) MouseClick("left", $aPos[0], $aPos[1]) Sleep(50) $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1) WEnd EndFunc What am I doing wrong?? I appreciate any help, I'm just stuck! Link to comment Share on other sites More sharing options...
Danp2 Posted February 27 Share Posted February 27 From the remarks of GUICtrlSetOnEvent help file entry -- Quote OnEvent functions are only called when the option GUIOnEventMode is set to 1 - when in this mode GUIGetMsg() is NOT used at all. To use this function, you would need to add the following to the top of your script and change your While loop to eliminate GUIGetMsg(). Opt("GUIOnEventMode", 1) WebDriver UDF [GH&S] Latest version Wiki FAQs Link to comment Share on other sites More sharing options...
mistersquirrle Posted February 27 Share Posted February 27 Like @Danp2 said, if you want to use GUICtrlSetOnEvent, then you need to also set OnEventMode as they showed. Otherwise, you need to in your Switch GUIGetMsg look for the button and call your function, like: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; Create the main GUI window Global $hGUI = GUICreate("Color Clicker", 300, 150) ; Create a label for the hex color code text field GUICtrlCreateLabel("Enter a hex color code (e.g. FFFFFF):", 10, 10, 280, 20) ; Create a text field for the hex color code Global $hTextField = GUICtrlCreateInput("", 10, 30, 280, 20) ; Create a button to start the color clicker Local $hButton = GUICtrlCreateButton("Click Color", 10, 60, 80, 30) ; Show the GUI window GUISetState(@SW_SHOW) ; GUI event loop While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $hButton ButtonClick() EndSwitch WEnd ; Button click event handler Func ButtonClick() ; Get the hex color code from the text field Local $sHexCode = GUICtrlRead($hTextField) ; Convert the hex color code to a decimal color value Local $iColor = Hex($sHexCode, 6) ; Search the screen for pixels of the specified color and click them MsgBox(0, 'ButtonClick', 'Button clicked!') ;~ Local $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1) ;~ While IsArray($aPos) ;~ MouseClick("left", $aPos[0], $aPos[1]) ;~ Sleep(50) ;~ $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1) ;~ WEnd EndFunc ;==>ButtonClick Also be careful with your While IsArray...PixelSearch section, as if you're searching for a color that doesn't change when you click on it you'll be stuck in an infinite loop. We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Tyrus Posted February 28 Author Share Posted February 28 Awesome thanks guys! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now