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!

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

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