autohotkeyautohotkey-2

How do MsgBoxes and GUIs work in autohotkey v2


ok so I have this code

#Requires AutoHotkey v2.0

global MyGui := ""
global uNID := "", Password := ""
global EditUsername := "", EditPassword := ""
global CurrentDay := A_DD
global Oct31MessageShown := false
global SavesFilePath := A_ScriptDir . "\\julia present\\Julias present saves"

^END::Reload
^+END::ExitApp
^!T::MsgBox("Test hotkey works!")

^+U:: {
Run("https://go.utah.edu/cas/login?service=https%3A%2F%2Fincommon2.sso.utah.edu%2Fidp%2FAuthn%2FExternal%3Fconversation%3De1s1&entityId=http%3A%2F%2Futah.instructure.com%2Fsaml2")

    ; Wait for the page to load (adjust the sleep time if needed)
    Sleep(1000)
    
    ; Read the saved credentials
    savedUNID := ReadSavedCredential("uNID")
    savedPassword := ReadSavedCredential("Password")
    
    ; Ensure the window is active
    if WinExist("A")
        WinActivate
    
    ; Paste the username
    if (savedUNID != "") {
        SendInput(savedUNID)
        Sleep(100)  ; Increased delay before tabbing to next field
        SendInput("{Tab}")
    }
    
    ; Paste the password
    if (savedPassword != "") {
        Sleep(100)  ; Increased delay before pasting password
        SendInput(savedPassword)
        Sleep(100)  ; Increased delay before pressing Enter
        SendInput("{Enter}")
    }

}

^+!U:: {
global MyGui, uNID, Password, EditUsername, EditPassword

    MyGui := Gui()
    MyGui.Add("Text",, "Please enter your U of U ID:")
    EditUsername := MyGui.Add("Edit", "vuNID w200")
    MyGui.Add("Text",, "Please enter your U of U Password:")
    EditPassword := MyGui.Add("Edit", "vPassword w200 Password")
    SubmitButton := MyGui.Add("Button", "w100", "Submit").OnEvent("Click", ButtonSubmit)
    
    ; Create a hotkey that's only active when the GUI is open
    HotIfWinactive("ahk_class AutoHotkeyGUI")
    Hotkey("Enter", HandleEnter)
    
    MyGui.Show()

}

ButtonSubmit(\*) {
; Retrieve the values from the GUI controls
savedUNID := EditUsername.Text
savedPassword := EditPassword.Text

    ; Close the GUI
    MyGui.Destroy()
    
    ; Save the credentials to file
    SaveCredentials(savedUNID, savedPassword)
    
    ; Display the entered information
    MsgBox("Credentials saved successfully!`nuNID: " . savedUNID . "`nPassword: " . savedPassword)
    
    ; Update global variables
    global uNID := savedUNID
    global Password := savedPassword

}

; Function to handle Enter key press
HandleEnter(\*) {
if (MyGui.FocusedCtrl = EditUsername) {
EditPassword.Focus()
} else if (MyGui.FocusedCtrl = EditPassword) {
ButtonSubmit()
}
}

; Function to save credentials to the file
SaveCredentials(uNID, Password) {
try {
; Clear the file by opening it in write mode (which truncates the file)
fileObj := FileOpen(SavesFilePath, "w")
if (fileObj) {
; Write the new credentials
fileObj.Write("uNID:" . uNID . "\`n")
fileObj.Write("Password:" . Password)
fileObj.Close()
} else {
throw Error("Unable to open file for writing")
}
} catch as err {
MsgBox("Error saving credentials: " . err.Message)
}
}

; Function to read a specific credential from the file
ReadSavedCredential(credentialType) {
try {
fileContent := FileRead(SavesFilePath)
lines := StrSplit(fileContent, "\`n")
for line in lines {
if (InStr(line, credentialType . ":") == 1) {
return StrSplit(line, ":")\[2\]
}
}
} catch as err {
MsgBox("Error reading credentials: " . err.Message)
}
return ""
}

UpdateCurrentDay() {
global CurrentDay, Oct31MessageShown
newDay := A_DD
MsgBox("current day" . CurrentDay . "newDay" . newDay)
if (newDay != CurrentDay) {
CurrentDay := newDay

        ; Reset the flag if it's a new day
        Oct31MessageShown := false
        
        ; Check if it's October 31st, 2024
        if (A_MM = "9" and A_DD = "4" and !Oct31MessageShown) {
            SoundBeep
            MsgBox("About to call OpenSpecialWindow")
            OpenSpecialWindow()
            Oct31MessageShown := true
        }
    }

}

OpenSpecialWindow() {
MsgBox("OpenSpecialWindow function called")

    specialGui := Gui()
    specialGui.Add("Text",, "Happy Birthday Julia, and Happy Halloween")
    specialGui.Add("Text",, "This is a special message for October 31st")
    specialGui.Add("Button", "w100", "Close").OnEvent("Click", (*) => specialGui.Destroy())
    
    specialGui.Show()
    
    MsgBox("GUI should be visible now")

}

SetTimer(UpdateCurrentDay, 60000)
UpdateCurrentDay()

for some reason the only GUI and Msgboxes that shows up are in ^+U and ^+!U function.

Why do those functions work but not ^!T::(MsgBox("Test hotkey works"))

I've tried adding Msgboxes basically everywhere and they only ever open in those functions. I even tried having that outside of a function and that didn't show up when then project was loading.


Solution

  • Lines 63, 84 and 117. Here is working the msgbox with ^!T

    #Requires AutoHotkey v2.0
    
    global MyGui := ""
    global uNID := "", Password := ""
    global EditUsername := "", EditPassword := ""
    global CurrentDay := A_DD
    global Oct31MessageShown := false
    global SavesFilePath := A_ScriptDir . "\\julia present\\Julias present saves"
    
    ^END::Reload
    ^+END::ExitApp
    ^!T::MsgBox("Test hotkey works!")
    
    ^+U:: {
    Run("https://go.utah.edu/cas/login?service=https%3A%2F%2Fincommon2.sso.utah.edu%2Fidp%2FAuthn%2FExternal%3Fconversation%3De1s1&entityId=http%3A%2F%2Futah.instructure.com%2Fsaml2")
    
        ; Wait for the page to load (adjust the sleep time if needed)
        Sleep(1000)
        
        ; Read the saved credentials
        savedUNID := ReadSavedCredential("uNID")
        savedPassword := ReadSavedCredential("Password")
        
        ; Ensure the window is active
        if WinExist("A")
            WinActivate
        
        ; Paste the username
        if (savedUNID != "") {
            SendInput(savedUNID)
            Sleep(100)  ; Increased delay before tabbing to next field
            SendInput("{Tab}")
        }
        
        ; Paste the password
        if (savedPassword != "") {
            Sleep(100)  ; Increased delay before pasting password
            SendInput(savedPassword)
            Sleep(100)  ; Increased delay before pressing Enter
            SendInput("{Enter}")
        }
    
    }
    
    ^+!U:: {
    global MyGui, uNID, Password, EditUsername, EditPassword
    
        MyGui := Gui()
        MyGui.Add("Text",, "Please enter your U of U ID:")
        EditUsername := MyGui.Add("Edit", "vuNID w200")
        MyGui.Add("Text",, "Please enter your U of U Password:")
        EditPassword := MyGui.Add("Edit", "vPassword w200 Password")
        SubmitButton := MyGui.Add("Button", "w100", "Submit").OnEvent("Click", ButtonSubmit)
        
        ; Create a hotkey that's only active when the GUI is open
        HotIfWinactive("ahk_class AutoHotkeyGUI")
        Hotkey("Enter", HandleEnter)
        
        MyGui.Show()
    
    }
    
    ButtonSubmit(*) {   ;   REMOVED unnecessary "\"
    ; Retrieve the values from the GUI controls
    savedUNID := EditUsername.Text
    savedPassword := EditPassword.Text
    
        ; Close the GUI
        MyGui.Destroy()
        
        ; Save the credentials to file
        SaveCredentials(savedUNID, savedPassword)
        
        ; Display the entered information
        MsgBox("Credentials saved successfully!`nuNID: " . savedUNID . "`nPassword: " . savedPassword)
        
        ; Update global variables
        global uNID := savedUNID
        global Password := savedPassword
    
    }
    
    ; Function to handle Enter key press
    HandleEnter(*) {    ;   REMOVED unnecessary "\"
    if (MyGui.FocusedCtrl = EditUsername) {
    EditPassword.Focus()
    } else if (MyGui.FocusedCtrl = EditPassword) {
    ButtonSubmit()
    }
    }
    
    ; Function to save credentials to the file
    SaveCredentials(uNID, Password) {
    try {
    ; Clear the file by opening it in write mode (which truncates the file)
    fileObj := FileOpen(SavesFilePath, "w")
    if (fileObj) {
    ; Write the new credentials
    fileObj.Write("uNID:" . uNID . "\`n")
    fileObj.Write("Password:" . Password)
    fileObj.Close()
    } else {
    throw Error("Unable to open file for writing")
    }
    } catch as err {
    MsgBox("Error saving credentials: " . err.Message)
    }
    }
    
    ; Function to read a specific credential from the file
    ReadSavedCredential(credentialType) {
    try {
    fileContent := FileRead(SavesFilePath)
    lines := StrSplit(fileContent, "\`n")
    for line in lines {
    if (InStr(line, credentialType . ":") == 1) {
    return StrSplit(line, ":")[2]   ;   REMOVED unnecessary "\"
    }
    }
    } catch as err {
    MsgBox("Error reading credentials: " . err.Message)
    }
    return ""
    }
    
    UpdateCurrentDay() {
    global CurrentDay, Oct31MessageShown
    newDay := A_DD
    MsgBox("current day" . CurrentDay . "newDay" . newDay)
    if (newDay != CurrentDay) {
    CurrentDay := newDay
    
            ; Reset the flag if it's a new day
            Oct31MessageShown := false
            
            ; Check if it's October 31st, 2024
            if (A_MM = "9" and A_DD = "4" and !Oct31MessageShown) {
                SoundBeep
                MsgBox("About to call OpenSpecialWindow")
                OpenSpecialWindow()
                Oct31MessageShown := true
            }
        }
    
    }
    
    OpenSpecialWindow() {
        MsgBox("OpenSpecialWindow function called")
        
            specialGui := Gui()
            specialGui.Add("Text",, "Happy Birthday Julia, and Happy Halloween")
            specialGui.Add("Text",, "This is a special message for October 31st")
            specialGui.Add("Button", "w100", "Close").OnEvent("Click", (*) => specialGui.Destroy())
            
            specialGui.Show()
            
            MsgBox("GUI should be visible now")
        
        }
        
        SetTimer(UpdateCurrentDay, 60000)
        UpdateCurrentDay()