Jump to content

Recommended Posts

Posted

Good day,

To begin with, I DO understand that you are unable to pass parameters employing "HotKeySet".

I have looked at, and considered, a number of potential "resolves" - which are, sad-to-say...way above my pay grade!

Anyhow, I do have the following example:

Spoiler
; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; -----------------------------------------------
Opt("MustDeclareVars", 1)
Opt("MouseCoordMode", 0)
; -----------------------------------------------
Global $iExtTimeOut = 2000
; ---------------------
Global $LblHotKey
Global $mActivateHotKey
; -----------------------------------------------
GetInput()
; -----------------------------------------------
Func GetInput()
    Local $sPrompt = "Notice!"
    Local $sPromptMessage = "Enter the [name]:" & @CRLF & _
            "[1] name1" & @CRLF & _
            "[2] name2" & @CRLF
    ; -----------------
    Local $sResult = InputBox($sPrompt, $sPromptMessage, "", " ", 200, 150)
    ; -----------------------------------------------
    If @error Then
        Exit
    EndIf
    ; -----------------------------------------------
    If $sResult = "1" Then
        MainT1()
    EndIf
    If $sResult = 2 Then
        MainT2()
    EndIf
EndFunc   ;==>GetInput
; -----------------------------------------------
; For Main Type_1
; -----------------------------------------------
Func MainT1()
    ConsoleWrite("MainT1" & @CRLF)
    Local $hMainGUI = GUICreate(" Menu Title", 125, 19, 1500, 19)
    WinSetOnTop(" Menu", "", $WINDOWS_ONTOP)
    ; -----------------------------------------------
    Local $mMain = GUICtrlCreateMenu("HotKey")
    Local $mActivateHotKey = GUICtrlCreateMenuItem("Activate", $mMain)
    ; -----------------------------------------------
    GUISetState(@SW_SHOW)
    ; -----------------------------------------------
    While 1
        Local $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
                ; -----------------
            Case $mActivateHotKey
                CreateLabelT1()
        EndSwitch
    WEnd
EndFunc   ;==>MainT1
; -----------------------------------------------
Func CreateLabelT1()
    $mActivateHotKey = GUICreate("", 150, 20, 115, 52, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetFont(14, 800, 0, "Calibri")
    GUISetBkColor(0x3D3D3D)
    ; -----------------------------------------------
    $LblHotKey = GUICtrlCreateLabel("Activate HotKey  ", 0, 0, 150, 20, $SS_CENTER)
    GUICtrlSetColor(-1, 0xFFFFFF)
    ; -----------------------------------------------
    GUISetState(@SW_SHOW)
    ; -----------------------------------------------
    While 1
        Switch GUIGetMsg()
            Case $LblHotKey
                Local $hbActivateHotKey = ActivateHotKeyT1()
                If $hbActivateHotKey = True Then ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>CreateLabelT1
; -----------------------------------------------
Func ActivateHotKeyT1()
    Local Static $hbActivateHotKey = True
    ; -----------------------------------------------
    If $hbActivateHotKey = True Then
        HotKeySet("{`}", CallT1)
        GUICtrlSetData($LblHotKey, "Deactivate HotKey")
        $hbActivateHotKey = False
        ToolTip("Employ the Grave Key [`]...", 192, 68, "The HotKey feature is now activated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffT1, $iExtTimeOut)
    Else
        HotKeySet("{`}")
        GUICtrlSetData($LblHotKey, "Activate HotKey")
        GUIDelete($mActivateHotKey)
        $hbActivateHotKey = True
        ToolTip("Exiting HotKey feature...", 192, 68, "The HotKey feature is now deactivated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffT1, $iExtTimeOut)
    EndIf
    Return $hbActivateHotKey
EndFunc   ;==>ActivateHotKeyT1
; -----------------------------------------------
Func ToolTipOffT1()
    AdlibUnRegister(ToolTipOffT2)
    ToolTip("")
EndFunc   ;==>ToolTipOffT1
; -----------------------------------------------
Func CallT1()
    ConsoleWrite("CallT1" & @CRLF)
EndFunc   ;==>CallT1
; -----------------------------------------------
; For Main Type_2
; -----------------------------------------------
Func MainT2()
    ConsoleWrite("MainT2" & @CRLF)
    Local $hMainGUI = GUICreate(" Menu Title", 125, 19, 1500, 19)
    WinSetOnTop(" Menu", "", $WINDOWS_ONTOP)
    ; -----------------------------------------------
    Local $mMain = GUICtrlCreateMenu("HotKey")
    Local $mActivateHotKey = GUICtrlCreateMenuItem("Activate", $mMain)
    ; -----------------------------------------------
    GUISetState(@SW_SHOW)
    ; -----------------------------------------------
    While 1
        Local $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
                ; -----------------
            Case $mActivateHotKey
                CreateLabelT2()
        EndSwitch
    WEnd
EndFunc   ;==>MainT2
; -----------------------------------------------
Func CreateLabelT2()
    $mActivateHotKey = GUICreate("", 150, 20, 115, 52, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetFont(14, 800, 0, "Calibri")
    GUISetBkColor(0x3D3D3D)
    ; -----------------------------------------------
    $LblHotKey = GUICtrlCreateLabel("Activate HotKey  ", 0, 0, 150, 20, $SS_CENTER)
    GUICtrlSetColor(-1, 0xFFFFFF)
    ; -----------------------------------------------
    GUISetState(@SW_SHOW)
    ; -----------------------------------------------
    While 1
        Switch GUIGetMsg()
            Case $LblHotKey
                Local $hbActivateHotKey = ActivateHotKeyT2()
                If $hbActivateHotKey = True Then ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>CreateLabelT2
; -----------------------------------------------
Func ActivateHotKeyT2()
    Local Static $hbActivateHotKey = True
    ; -----------------------------------------------
    If $hbActivateHotKey = True Then
        HotKeySet("{`}", CallT2)
        GUICtrlSetData($LblHotKey, "Deactivate HotKey")
        $hbActivateHotKey = False
        ToolTip("Employ the Grave Key [`]...", 192, 68, "The HotKey feature is now activated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffT2, $iExtTimeOut)
    Else
        HotKeySet("{`}")
        GUICtrlSetData($LblHotKey, "Activate HotKey")
        GUIDelete($mActivateHotKey)
        $hbActivateHotKey = True
        ToolTip("Exiting HotKey feature...", 192, 68, "The HotKey feature is now deactivated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffT2, $iExtTimeOut)
    EndIf
    Return $hbActivateHotKey
EndFunc   ;==>ActivateHotKeyT2
; -----------------------------------------------
Func ToolTipOffT2()
    AdlibUnRegister(ToolTipOffT2)
    ToolTip("")
EndFunc   ;==>ToolTipOffT2
; -----------------------------------------------
Func CallT2()
    ConsoleWrite("CallT2" & @CRLF)
EndFunc   ;==>CallT2
; -----------------------------------------------

 

Based on the above example, I have the following 2 questions:

Q1: Can the above be "streamlined"?
Q2: How can I return tot he calling function [GetInput()]...without having to exit the functions first?

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

 

Posted

Hi @mr-es335,

A1: Yes, to what degree, depends on how unique each T case is (T1, T2).
A2: You can't. You need to exit/return from the function, to return and/or process the response.

  • You CAN return early and preserve most/all of your current behavior, if you either use GUIOnEventMode or keep your GUI event loop outside the smaller functions.
  • Since AutoIt is single threaded, you can't let the functions run, while getting and processing the result in GetInput. One way of faking thread-like behavior, is to fork you script, meaning one or more child processes with the a worker or same script runs and communicates via one of many IPC options, for example Windows messages.

I would suggest something like this:

#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("MouseCoordMode", 0)
Opt("GuiOnEventMode", 1)

Global Const $iExtTimeOut = 2000
Global $sTx = Null

GetInput()

Func GetInput()
    Local $sPrompt = "Notice!"
    Local $sPromptMessage = "Enter the [name]:" & @CRLF & _
            "[1] name1" & @CRLF & _
            "[2] name2" & @CRLF

    Local $sResult
    While 1
        $sResult = InputBox($sPrompt, $sPromptMessage, "", " ", 200, 150)

        If @error Then
            Exit
        EndIf

        Switch $sResult
            Case 1, 2
                ;Call("MainT"&$sResult)
                MainTx($sResult)
                ExitLoop
            Case Else
                MsgBox(0, "Invalid input", "Invalid input!" & @CRLF & "Please try again.")
        EndSwitch
    WEnd

    While 1
        Sleep(10); Keep the script running
    Wend
EndFunc

Func MainTx($x)
    ConsoleWrite("MainT" & $x &@CRLF)
    $sTx = $x

    Local $hMainGUI = GUICreate(" Menu Title", 125, 19, 1500, 19)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hMainGUI)
        WinSetOnTop($hMainGUI, "", $WINDOWS_ONTOP)

    Local $mMain = GUICtrlCreateMenu("HotKey")
    Local $mActivateHotKey = GUICtrlCreateMenuItem("Activate", $mMain)
        GUICtrlSetOnEvent($mActivateHotKey, CreateLabelTx)

    GUISetState(@SW_SHOW)
EndFunc

Func CreateLabelTx()
    Local $mActivateHotKey = GUICreate("", 150, 20, 115, 52, $WS_POPUP, $WS_EX_TOPMOST)
        GUISetFont(14, 800, 0, "Calibri")
        GUISetBkColor(0x3D3D3D)

    Local $LblHotKey = GUICtrlCreateLabel("Activate HotKey  ", 0, 0, 150, 20, $SS_CENTER)
        GUICtrlSetOnEvent($LblHotKey, ActivateHotKeyTx)
        GUICtrlSetColor(-1, 0xFFFFFF)

    GUISetState(@SW_SHOW)
EndFunc

Func ActivateHotKeyTx()
    Local Static $bActivateHotkey = True
    Local $LblHotKey = @GUI_CtrlId
    Local $mActivateHotKey = @GUI_WinHandle

    If $bActivateHotkey Then
        HotKeySet("{`}", CallTx)
        GUICtrlSetData($LblHotKey, "Deactivate HotKey")
        ToolTip("Employ the Grave Key [`]...", 192, 68, "The HotKey feature is now activated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffTx, $iExtTimeOut)
    Else
        HotKeySet("{`}")
        GUICtrlSetData($LblHotKey, "Activate HotKey")
        GUIDelete($mActivateHotKey)
        ToolTip("Exiting HotKey feature...", 192, 68, "The HotKey feature is now deactivated!", 0, $TIP_BALLOON)
        AdlibRegister(ToolTipOffTx, $iExtTimeOut)
    EndIf

    $bActivateHotkey = Not $bActivateHotkey
EndFunc

Func CallTx()
    ConsoleWrite("CallT" & $sTx & @CRLF)
EndFunc

Func ToolTipOffTx()
    AdlibUnRegister(ToolTipOffTx)
    ToolTip("")
EndFunc

Func _Exit()
    Exit
EndFunc

I might be able to simplify it more, but again it all depends on how specialized each T case would be in the end.

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
×
×
  • Create New...