Jump to content

Recommended Posts

Posted

Good day,

Personally, I have never been satisfied with the current iteration of the EnableHotKey procedure that I have been employing over the past number of months.

As a result, I have "pieced together" what I believe to be a more representative of what I am looking for.

As I am a "neophyte" text-enterer [...I do NOT refer to myself as a "programmer", so as NOT to insult anyone...] I would appreciate it if someone would peruse the following sampling and provide me with your critique of that sampling.

Spoiler
; -----------------------------------------------
#RequireAdmin
; -----------------------------------------------
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; -----------------------------------------------
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
; -----------------------------------------------
Global $Form1, $mForm1Button
Global $Form2, $mForm2Button
Global $iMenuPosition, $sDisplayHotKey
Global $iWidth = 735, $iHeight = 48, $ixpos = -1, $iypos = -1, $iOpt = 1, $sFontName = "Corbel Bold", $iFontSize = 16, $iFontWt = 800
; -----------------------------------------------
Forms()
; -----------------------------------------------
Func Forms()
    Local $iFontSize = 14
    $Form1 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont($iFontSize, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm1Button = GUICtrlCreateButton("Enable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm1Button, "_ColRow")
    ; -----------------------------------------------
    Local $iFontSize = 14
    $Form2 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont($iFontSize, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm2Button = GUICtrlCreateButton("Disable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm2Button, "_ColRow")
    ; -----------------------------------------------
    GUISetState(@SW_SHOW, $Form1)
    ; -----------------------------------------------
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>Forms
; -----------------------------------------------
Func _ColRow()
    Switch @GUI_CtrlId
        Case $mForm1Button
            GUISetState(@SW_HIDE, $Form1)
            HotkeyEnabledStatus()
        Case $mForm2Button
            HotkeyDisabledStatus()
            Exit
    EndSwitch
EndFunc   ;==>_ColRow
; -----------------------------------------------
Func HotkeyEnabledStatus()
    MsgBox($MB_OK, "", "HotKey is now enabled", 1)
    EnableDisableHotKey()
EndFunc   ;==>HotkeyEnabledStatus
; -----------------------------------------------
Func HotkeyDisabledStatus()
    MsgBox($MB_OK, "", "HotKey is now disabled...", 1)
    EnableDisableHotKey()
EndFunc   ;==>HotkeyDisabledStatus
; -----------------------------------------------
Func EnableDisableHotKey()
    Local Static $bHotKeyEnabled = True
    ; -----------------------------------------------
    If $bHotKeyEnabled = True Then
        HotKeySet("{NUMPADSUB}", "UpdateF12SceneData") ; The script that is to be executed!
        $bHotKeyEnabled = False
    Else
        WinClose($sDisplayHotKey)
        HotKeySet("{NUMPADSUB}")
        $bHotKeyEnabled = True
    EndIf
    GUISetState(@SW_SHOW, $Form2)
    Return $bHotKeyEnabled
EndFunc   ;==>EnableDisableHotKey
; -----------------------------------------------
Func UpdateF12SceneData()
    MsgBox($MB_OK, "", " Update F12 Scene Data", 1) ; ⚠ Merely a placeholder...more to come! ⚠
EndFunc   ;==>UpdateF12SceneData
; -----------------------------------------------
Func _CloseForm()
    Switch @GUI_WinHandle
        Case $Form1
            Exit
        Case $Form2
            Exit
    EndSwitch
EndFunc   ;==>_CloseForm
; -----------------------------------------------

 

As always...any assistance in this matter would be greatly appreciated!

Thank you all for your time!

  • Solution
Posted (edited)

in my opinion you don't need so many windows
(one to activate, and one to deactivate)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Global $Form1, $mForm1Button
Global $iWidth = 735, $iHeight = 48, $ixpos = -1, $iypos = -1, $iOpt = 1, $sFontName = "Corbel Bold", $iFontSize = 16, $iFontWt = 800

Forms()

; -----------------------------------------------
Func Forms()
    $Form1 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont(14, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm1Button = GUICtrlCreateButton("Enable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm1Button, "_ColRow")
    GUISetState(@SW_SHOW, $Form1)
    ; -----------------
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>Forms
; -----------------------------------------------
Func _ColRow()
    Switch @GUI_CtrlId
        Case $mForm1Button
            _Togle_NUMPADSUB()
    EndSwitch
EndFunc   ;==>_ColRow
; -----------------------------------------------
Func _CloseForm()
    Switch @GUI_WinHandle
        Case $Form1
            Exit
    EndSwitch
EndFunc   ;==>_CloseForm
; -----------------------------------------------
Func _Togle_NUMPADSUB()
    Local Static $bEnabled = True
    Local $aGuiPos = WinGetPos($Form1)
    If $bEnabled Then
        HotKeySet("{NUMPADSUB}", "UpdateF12SceneData")
        _ToolTip(" ", $aGuiPos[0], $aGuiPos[1] - 10, "HotKey is now enabled", 1, 2000)
        GUICtrlSetData($mForm1Button, "Disable Hotkey")
        GUICtrlSetBkColor($mForm1Button, 0xCAE4FF) ; ⚠ extra color indicator
    Else
        HotKeySet("{NUMPADSUB}")
        _ToolTip(" ", $aGuiPos[0], $aGuiPos[1] - 10, "HotKey is now disabled...", 3, 2000)
        GUICtrlSetData($mForm1Button, "Enable Hotkey")
        GUICtrlSetBkColor($mForm1Button, 0xE1E1E1) ; ⚠extra color indicator
        Sleep(1500) ; give time to show the _ToolTip
        WinClose($Form1)
    EndIf
    $bEnabled = Not $bEnabled
EndFunc   ;==>_TogleF10
; -----------------------------------------------
Func UpdateF12SceneData()
    _ToolTip(" ", @DesktopWidth / 2, @DesktopHeight / 3, "UpdateF12SceneData  ... do your staff", 2)
EndFunc   ;==>_CallF10
; -----------------------------------------------
; ToolTip functions
Func _ToolTip($s_Text, $i_X = Default, $i_Y = Default, $s_Title = "", $i_Icon = 0, $i_Timeout = 1500, $iOptions = 0)
    AdlibUnRegister("_ToolTipKiller") ; This prevents the previous timer from closing the new tooltip.
    ToolTip($s_Text, $i_X, $i_Y, $s_Title, $i_Icon, $iOptions)
    AdlibRegister("_ToolTipKiller", $i_Timeout)
EndFunc   ;==>_ToolTip
; -----------------------------------------------
Func _ToolTipKiller()
    ToolTip("") ; Hide the tooltip
    AdlibUnRegister("_ToolTipKiller") ; unregister itself.
EndFunc
; -----------------------------------------------

 

Edit: I left the old ones so you can see the differences.

Spoiler
; -----------------------------------------------
;~ #RequireAdmin 
; -----------------------------------------------
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; -----------------------------------------------
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
; -----------------------------------------------
Global $Form1, $mForm1Button
Global $Form2, $mForm2Button
Global $iMenuPosition, $sDisplayHotKey
Global $iWidth = 735, $iHeight = 48, $ixpos = -1, $iypos = -1, $iOpt = 1, $sFontName = "Corbel Bold", $iFontSize = 16, $iFontWt = 800
; -----------------------------------------------
Forms()
; -----------------------------------------------
Func Forms()
;~     Local $iFontSize = 14 ; ⚠ *** no longer needed
    $Form1 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont(14, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm1Button = GUICtrlCreateButton("Enable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm1Button, "_ColRow")
    ; -----------------------------------------------
;~     Local $iFontSize = 14 
;~     $Form2 = GUICreate(" ", 150, 40, 1270, 20)  ; ⚠ *** no longer needed
;~     GUISetFont($iFontSize, $iFontWt, $GUI_FONTNORMAL, $sFontName)
;~     GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
;~     $mForm2Button = GUICtrlCreateButton("Disable Hotkey", 10, 10, 130, 20)
;~     GUICtrlSetOnEvent($mForm2Button, "_ColRow")
    ; -----------------------------------------------
    GUISetState(@SW_SHOW, $Form1)
    ; -----------------------------------------------
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>Forms
; -----------------------------------------------
Func _ColRow()
    Switch @GUI_CtrlId
        Case $mForm1Button
;~             GUISetState(@SW_HIDE, $Form1)
                EnableDisableHotKey() ; ⚠ new *** call direct from here
;~             HotkeyEnabledStatus()
;~         Case $mForm2Button
;~             HotkeyDisabledStatus()
;~             Exit
    EndSwitch
EndFunc   ;==>_ColRow
; -----------------------------------------------
;~ Func HotkeyEnabledStatus() ; ⚠ *** no longer needed
;~     MsgBox($MB_OK, "", "HotKey is now enabled", 1)
;~     EnableDisableHotKey()
;~ EndFunc   ;==>HotkeyEnabledStatus
; -----------------------------------------------
;~ Func HotkeyDisabledStatus() ; ⚠ *** no longer needed
;~     MsgBox($MB_OK, "", "HotKey is now disabled...", 1)
;~     EnableDisableHotKey()
;~ EndFunc   ;==>HotkeyDisabledStatus
; -----------------------------------------------
Func EnableDisableHotKey()
    Local Static $bHotKeyEnabled = True
    ; -----------------------------------------------
    If $bHotKeyEnabled = True Then
        MsgBox($MB_OK, "", "HotKey is now enabled", 1) ; ⚠ new *** to remove HotkeyEnabledStatus()
        HotKeySet("{NUMPADSUB}", "UpdateF12SceneData") ; The script that is to be executed!
        GUICtrlSetData($mForm1Button, "Disable Hotkey") ; ⚠new *** to remove $Form2
;~         $bHotKeyEnabled = False ; ⚠ *** We call it once at the end, with $bHotKeyEnabled = Not $bHotKeyEnabled
    Else
        MsgBox($MB_OK, "", "HotKey is now disabled...", 1) ; ⚠new *** to remove HotkeyDisabledStatus()
;~         WinClose($sDisplayHotKey) ; ⚠ ??? declred but not used
        HotKeySet("{NUMPADSUB}")
        GUICtrlSetData($mForm1Button, "Enable Hotkey") ; ⚠new *** to remove $Form2
;~         $bHotKeyEnabled = True ; ⚠ *** We call it once at the end, with $bHotKeyEnabled = Not $bHotKeyEnabled
        WinClose($Form1)
    EndIf
    $bHotKeyEnabled = Not $bHotKeyEnabled
    Return Not $bHotKeyEnabled
EndFunc   ;==>EnableDisableHotKey
; -----------------------------------------------
Func UpdateF12SceneData()
    MsgBox($MB_OK, "", " Update F12 Scene Data", 1) ; ⚠ Merely a placeholder...more to come! ⚠
EndFunc   ;==>UpdateF12SceneData
; -----------------------------------------------
Func _CloseForm()
    Switch @GUI_WinHandle
        Case $Form1
            Exit
;~         Case $Form2 ; ⚠ *** no longer needed
;~             Exit
    EndSwitch
EndFunc   ;==>_CloseForm
; -----------------------------------------------

 

 

Edited by ioa747

I know that I know nothing

Posted (edited)

ioa747,

Firstly, thank for the update for the first sampling
Secondly, thanks for the commenting of the original sampling
Thirdly, was any of my original example of any value whatsoever?

In particular, if I my intention is to exit the script upon selection of "Disable Hotkey" , would the following NOT BE a workable solution?

Func _Toggle_NUMPADSUB()
    Local Static $bEnabled = True
    Local $aGuiPos = WinGetPos($Form1)
    If $bEnabled Then
        HotKeySet("{NUMPADSUB}", "UpdateF12SceneData")
        GUICtrlSetData($mForm1Button, "Disable Hotkey")
    Else
        WinClose($Form1)
    EndIf
    $bEnabled = Not $bEnabled
EndFunc   ;==>_Toggle_NUMPADSUB

Lastly, what is the difference - if any, between "WinClose($Form1)" and "Exit"?

Thanks again, ioa747...

Edited by mr-es335
Posted (edited)

Yes, your original example was perfectly fine.

Spoiler

 

7 hours ago, mr-es335 said:

was any of my original example of any value whatsoever?

 

Yes, your original example was perfectly fine.
It was a clear and functional approach to toggling the state of a Hotkey and managing the GUI with two separate forms.

My modification aimed at, condensing
I used a form and a button for the toggle, making the code simpler, and easier to read.

I integrated the _ToolTip function for more "elegant" user information,
instead of MsgBox, which interrupts the flow, taking the focus away from the main script.
 

7 hours ago, mr-es335 said:

what is the difference - if any, between "WinClose($Form1)" and "Exit"?

This is something I expected you to know, given your Experience.

The main difference is, that "Exit" closes the entire script,
while "WinClose($Form1)" , closes the "$Form1" GUI, and returns the flow to what follows after the call to Forms().

If nothing follows then the script will terminate

this has more to do with whether this script is part of a larger one, and there is a continuation after the call to Forms()

If not
Yes, the solution you propose is perfectly functional and correct if the purpose is to exit the script when the Hotkey is deactivated.


If I were to go by the logic of the two forms, I would go something like this

; -----------------------------------------------
;~ #RequireAdmin
; -----------------------------------------------
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; -----------------------------------------------
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
; -----------------------------------------------
Global $Form1, $mForm1Button
Global $Form2, $mForm2Button
Global $iMenuPosition, $sDisplayHotKey
Global $iWidth = 735, $iHeight = 48, $ixpos = -1, $iypos = -1, $iOpt = 1, $sFontName = "Corbel Bold", $iFontSize = 16, $iFontWt = 800
; -----------------------------------------------
Forms()
; -----------------------------------------------
Func Forms()
    Local $iFontSize = 14
    $Form1 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont($iFontSize, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm1Button = GUICtrlCreateButton("Enable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm1Button, "_ColRow")
    ; -----------------------------------------------
    Local $iFontSize = 14
    $Form2 = GUICreate(" ", 150, 40, 1270, 20)
    GUISetFont($iFontSize, $iFontWt, $GUI_FONTNORMAL, $sFontName)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseForm")
    $mForm2Button = GUICtrlCreateButton("Disable Hotkey", 10, 10, 130, 20)
    GUICtrlSetOnEvent($mForm2Button, "_ColRow")
    ; -----------------------------------------------
    GUISetState(@SW_SHOW, $Form1)
    ; -----------------------------------------------
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>Forms
; -----------------------------------------------
Func _ColRow()
    Switch @GUI_CtrlId
        Case $mForm1Button
            MsgBox($MB_OK, "", "HotKey is now enabled", 1)
            GUISetState(@SW_HIDE, $Form1)
            GUISetState(@SW_SHOW, $Form2) ; ⚠ By hiding $Form1, we show $Form2
            ;HotkeyEnabledStatus()
            HotKeySet("{NUMPADSUB}", "UpdateF12SceneData") ; The script that is to be executed!
        Case $mForm2Button
            MsgBox($MB_OK, "", "HotKey is now disabled...", 1)
            ; HotkeyDisabledStatus()
            ; HotKeySet("{NUMPADSUB}") ; ⚠ since it has Exit here, you don't need to deactivate it
            Exit
    EndSwitch
EndFunc   ;==>_ColRow
; -----------------------------------------------
;~ Func HotkeyEnabledStatus()
;~     MsgBox($MB_OK, "", "HotKey is now enabled", 1)
;~     EnableDisableHotKey()
;~ EndFunc   ;==>HotkeyEnabledStatus
;~ ; -----------------------------------------------
;~ Func HotkeyDisabledStatus()
;~     MsgBox($MB_OK, "", "HotKey is now disabled...", 1)
;~     EnableDisableHotKey()
;~ EndFunc   ;==>HotkeyDisabledStatus
;~ ; -----------------------------------------------
;~ Func EnableDisableHotKey()
;~     Local Static $bHotKeyEnabled = True
;~     ; -----------------------------------------------
;~     If $bHotKeyEnabled = True Then
;~         HotKeySet("{NUMPADSUB}", "UpdateF12SceneData") ; The script that is to be executed!
;~         $bHotKeyEnabled = False
;~     Else
;~         WinClose($sDisplayHotKey)
;~         HotKeySet("{NUMPADSUB}")
;~         $bHotKeyEnabled = True
;~     EndIf
;~     GUISetState(@SW_SHOW, $Form2)
;~     Return $bHotKeyEnabled
;~ EndFunc   ;==>EnableDisableHotKey
;~ ; -----------------------------------------------
Func UpdateF12SceneData()
    MsgBox($MB_OK, "", " Update F12 Scene Data", 1) ; ⚠ Merely a placeholder...more to come! ⚠
EndFunc   ;==>UpdateF12SceneData
; -----------------------------------------------
Func _CloseForm()
    Switch @GUI_WinHandle
        Case $Form1
            Exit
        Case $Form2
            Exit
    EndSwitch
EndFunc   ;==>_CloseForm
; -----------------------------------------------

Do you see how much extra code I got rid of?  :)

 

Τhe general idea behind Togle is, that the same function, once opens it, once closes it.

So if I have a Togle function, why do I want two buttons?
or if I have two buttons, why do I have a Togle function?

I hope I was understandable
(Because it took me longer to write the explanation, than the script)  :)

 

 

Edited by ioa747

I know that I know nothing

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