Skip to content

Instantly share code, notes, and snippets.

@TheDcoder
Last active December 11, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheDcoder/3fa733ce760ded26e5e5c873c75fc6fe to your computer and use it in GitHub Desktop.
Save TheDcoder/3fa733ce760ded26e5e5c873c75fc6fe to your computer and use it in GitHub Desktop.
Generate custom/fake/pseudo events in OnEvent mode in AutoIt
; This snippet will show you a "hack" that you can use in GUI OnEvent mode in AutoIt
; With this hack you can create fake/pseudo events for a control in a OnEvent "handler"
; function which is registered with multiple controls.
; Usually you can get around this by using a unique function for every control
; but I like to use a single function that I use as a handler for a group of related
; controls. Looks neat and organized :)
; Let's get started!
; Include the important stuff
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
; This function will create the GUI and make it visible
Func CreateGUI()
GUICreate("Example", 60, 60)
; Functions with parameters will not be called, unless all of their parameters are optional (undocumented)
GUISetOnEvent($GUI_EVENT_CLOSE, HandleGUI)
Global $idToggleButton = GUICtrlCreateButton("", 5, 05, 50, 50)
GUICtrlSetOnEvent($idToggleButton, HandleGUI)
EndFunc
; This is our most important piece of code, the Handler function which handles
; any events generated by the GUI!
Func HandleGUI($iCtrlID = Default)
; As mentioned previously, when a function is called by AutoIt's OnEvent handler,
; the optional parameters are NOT defined at all! So their default values are meaningless
; Therefore we can use IsDeclared to see if a parameter is declared, effective way to know
; if it was called by AutoIt's OnEvent handler.
;
; The switch expression is a ternary operation, it first checkes if $iCtrlID is defined,
; then if it is declared locally, the expression is evaluated to $iCtrlID's value. This
; would be the case when a psedo/fake event has been generated by the script manually
;
; If it was called by AutoIt, then the expression would evaluate to @GUI_CtrlID
;
; This allows for seamless integration with the code, you won't have to use ControlClick to
; create a mouse press or something similar!
Switch (IsDeclared("iCtrlID") = $DECLARED_LOCAL ? $iCtrlID : @GUI_CtrlId)
Case $GUI_EVENT_CLOSE
Exit
Case $idToggleButton
Local Static $bOn = True
If $bOn Then
GUICtrlSetData($idToggleButton, "Off")
$bOn = False
Else
GUICtrlSetData($idToggleButton, "On")
$bOn = True
EndIf
EndSwitch
EndFunc
; A practical use case:
; Often when there is a button which toggles/switches between values, the default value will have
; to be repeated when creating the control, this is fine for small things but is a good way to do
; if you have something more that needs to be done. This is just a simple example of a button which
; toggles between On and Off. The speciality is that those values will not be repeated in the code :)
;
; This is not very easy to read or as simple as setting the value of the button to the default state
; during the creation of the GUI, but it is a good practise that helps you avoid replicating text
CreateGUI()
HandleGUI($idToggleButton) ; This will generate a fake event which will trigger the same action as clicking on the button
GUISetState() ; Show the GUI
While True
Sleep(10)
WEnd
; Link to this code snippet on Gist: https://git.io/vbRQ5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment