Search code examples
if-statementuser-interfaceautohotkey

How can I put a ternary operator in the GuiControl command in AHK?


I'm trying to make a form screen that resets all of the fields when the "Add" or "Edit" buttons are clicked. Whenever the function is run, I want to get the row value of the selected row. When I click the "Add" button, I want to reset all of the fields. When I click "Edit", I want the currently selected row as RowValue. The first step, though, is making this work. Here is my code:

MainScript.ahk

SetWorkingDir, %A_ScriptDir%
SetNumLockState, AlwaysOn
SetCapsLockState, AlwaysOff

; Variables
OrangeMode := False
IniRead GuideShowing, Preferences.ini, Toggles, ShowGuide, True
IniRead, EmuNumpad, Preferences.ini, Toggles, EmulatedNumericalPad, False

KeybindArray := [[]]
KeybindArray[1][1] := 1
KeybindArray[1][2] := "G"
KeybindArray[1][3] := 4
KeybindArray[1][4] := 1
KeybindArray[1][5] := 4
KeybindArray[1][6] := 4
KeybindArray[1][7] := 4
KeybindArray[1][8] := 3
KeybindArray[1][9] := 2
KeybindArray[1][10] := 1
KeybindArray[1][11] := ""
KeybindArray[1][12] := ""
KeybindArray[1][13] := "https://verylong.websitename.com"

; Tags
#Persistent
#Include GuiScript.ahk

; Functions
SetGui(Name, RowValue=0) {
    If (Name = "Keybind") {
        Keybind_Key_Main := % (RowValue=0 ? 0 : KeybindArray[RowValue][1])
        Keybind_Key_Win := % (RowValue=0 ? 1 : KeybindArray[RowValue][2])
        Keybind_Key_CapsLock := % (RowValue=0 ? 0 : KeybindArray[RowValue][3])
        Keybind_Key_Shift := % (RowValue=0 ? 1 : KeybindArray[RowValue][4])
        Keybind_Key_Alt := % (RowValue=0 ? 1 : KeybindArray[RowValue][5])
        Keybind_Key_Ctrl := % (RowValue=0 ? 1 : KeybindArray[RowValue][6])
        Keybind_Option_Type := % (RowValue=0 ? 1 : KeybindArray[RowValue][7])
        Keybind_Option_Mode := % (RowValue=0 ? 1 : KeybindArray[RowValue][8])
        Keybind_Option_GoBlue := % (RowValue=0 ? 0 : KeybindArray[RowValue][9])
        Keybind_Option_Remap := % (RowValue=0 ? 1 : KeybindArray[RowValue][10])
        Keybind_Option_Preset := % (RowValue=0 ? 1 : KeybindArray[RowValue][11])
        Keybind_Option_Website := % (RowValue=0 ? "https://" : KeybindArray[RowValue][12])
        
        GuiControl, Choose, Keybind_Key_Main, % Keybind_Key_Main
        GuiControl, Choose, Keybind_Key_Win, % Keybind_Key_Win
        GuiControl,, Keybind_Key_CapsLock, % Keybind_Key_CapsLock
        GuiControl, Choose, Keybind_Key_Shift, % Keybind_Key_Shift
        GuiControl, Choose, Keybind_Key_Alt, % Keybind_Key_Alt
        GuiControl, Choose, Keybind_Key_Ctrl, % Keybind_Key_Ctrl
        GuiControl, Choose, Keybind_Option_Type, % Keybind_Option_Type
        GuiControl, Choose, Keybind_Option_Mode, % Keybind_Option_Mode
        GuiControl,, Keybind_Option_GoBlue, % Keybind_Option_GoBlue
        GuiControl, Choose, Keybind_Option_Remap, % Keybind_Option_Remap
        GuiControl, Choose, Keybind_Option_Preset, % Keybind_Option_Preset
        GuiControl,, Keybind_Option_Website, % Keybind_Option_Website
    } Else If (Name = Text) {
; Not coded yet
    }
}
Build_Restart() {
    MsgBox The Build_Restart function has been run ; WIP
}

; Subroutines (G-Labels)
BuildRestart:
    Build_Restart()
    Return
KeybindMenu_Add:
    SetGui("Keybind", 0) ; Clear all form boxes
    Gui Keybind:Show, Center w400 h300, Add Keybind ; Open the Keybind Edit GUI with a new keybind
    Return
KeybindMenu_Edit:
    SetGui("Keybind", 0) ; Clear all form boxes
    Gui Keybind:Show, Center w400 h300, Edit Keybind ; Open the Keybind Edit GUI with the currently selected row
    Return
KeybindMenu_Del:
    Return
KeybindEdit_Save:
    Gui Keybind:Hide ; Hide the Keybind Edit GUI
    MsgBox, 52, Build and Restart, New changes will not take effect until they are applied.`nWould you like to Build and Restart?
    IfMsgBox Yes
        Build_Restart()
        Return
    Return
KeybindEdit_Cancel:
    Gui Keybind:Hide ; Hide the Keybind Edit GUI
    Return
Keybind_Option_Type-Changed:
    GuiControl, Hide, Keybind_Option_Remap
    GuiControl, Hide, Keybind_Option_Preset
    GuiControl, Hide, Keybind_Option_Website
    GuiControl, Hide, Keybind_Option_App
    GuiControl, Hide, Keybind_Option_Custom

    GuiControlGet, Keybind_Option_Type,, Keybind_Option_Type
    If (Keybind_Option_Type = 1) {
        GuiControl, Show, Keybind_Option_Remap
    } Else If (Keybind_Option_Type = 2) {
        GuiControl, Show, Keybind_Option_Preset
    } Else If (Keybind_Option_Type = 3) {
        GuiControl, Show, Keybind_Option_Website
    } Else If (Keybind_Option_Type = 4) {
        GuiControl, Show, Keybind_Option_App
    } Else If (Keybind_Option_Type = 5) {
        GuiControl, Show, Keybind_Option_Custom
    }
    Return
Keybind_Option_Mode-Changed:
    GuiControlGet, Keybind_Option_Mode,, Keybind_Option_Mode
    If (Keybind_Option_Mode = 1) OR (Keybind_Option_Mode = 2) {
        GuiControl, Enable, Keybind_Option_GoBlue
    } else {
        GuiControl,, Keybind_Option_GoBlue, 0
        GuiControl, Disable, Keybind_Option_GoBlue
    }
    Return
KeybindListAction:  
    If (A_GuiEvent == "I") AND ( InStr(ErrorLevel,"S",true) ) {
        KeybindCurrentSelection := %A_GuiEvent%

        GuiControl, Enable, KeybindMenu_Edit
        GuiControl, Enable, KeybindMenu_Del
    } Else If (A_GuiEvent == "I") AND ( InStr(ErrorLevel,"S",false)) {
        GuiControl, Disable, KeybindMenu_Edit
        GuiControl, Disable, KeybindMenu_Del
    }
    Return

; --KEYBINDS--
>^LButton::MButton ; RCtrl + LeftClick = MiddleClick
F9::OrangeMode:=!OrangeMode ; Toggle the orange mode
; When Orange Mode is active
#If OrangeMode
    /::
        GuideShowing:=!GuideShowing ; Toggle visibility of the orange mode guide
        IniWrite, %GuideShowing%, Preferences.ini, Toggles, ShowGuide ; Update the preferences file
        Return
    N::
        Run C:\Windows\system32\notepad.exe ; Open Notepad
        OrangeMode:=false ; Close Orange Mode
        Return
    Q::
        EmuNumpad:=!EmuNumpad ; Toggle the emulated numerical pad
        IniWrite, %EmuNumpad%, Preferences.ini, Toggles, EmulatedNumericalPad ; Update the preferences file
        Return
#If GetKeyState("CapsLock", "P") AND OrangeMode ; When CapsLock is pressed during Orange Mode
    A::Gui Settings:Show, Center w500 h400 ; Open the Settings GUI
    M::Run MainScript.ahk ; Restart the program
; When the Numerical Pad is being emulated
#If EmuNumpad ; When the numerical pad is emulated, use numerical keys when holding {Alt}
    !1::!Numpad1
    !2::!Numpad2
    !3::!Numpad3
    !4::!Numpad4
    !5::!Numpad5
    !6::!Numpad6
    !7::!Numpad7
    !8::!Numpad8
    !9::!Numpad9
    !0::!Numpad0

GuiScript.ahk

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

Gui Keybind:New, -MinimizeBox -MaximizeBox, Add/Edit Keybind ; Create Keybind Edit GUI
    Gui Keybind:Default
    Gui Font, s10, Lexend
    Gui Margin, 15, 15

    Gui Add, Text, x15 y15 w220 h40, Type a single character - "A"`nType a key's name - "Backspace"
    Gui Add, ComboBox, x230 y24 w140 h200 vKeybind_Key_Main, LButton|RButton|MButton|XButton1|XButton2|WheelDown|WheelUp|WheelLeft|WheelRight|Space|Tab|Enter|Escape|Backspace|Delete|Home|End|PgUp|PgDn|Up|Down|Left|Right|PrintScreen|Numpad0|Numpad1|Numpad2|Numpad3|Numpad4|Numpad5|Numpad6|Numpad7|Numpad8|Numpad9|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12|F13|F14|F15|F16|F17|F18|F19|F20|F21|F22|F23|F24
    Gui Add, DropDownList, x15 y70 w120 AltSubmit vKeybind_Key_Win, No Win||Left Win|Right Win|Both Win|Either Win
    Gui Add, CheckBox, x140 y73 w120 vKeybind_Key_CapsLock, CapsLock
    Gui Add, DropDownList, x265 y70 w120 AltSubmit vKeybind_Key_Shift, No Shift||Left Shift|Right Shift|Both Shift|Either Shift
    Gui Add, DropDownList, x15 y105 w120 AltSubmit vKeybind_Key_Alt, No Alt||Left Alt|Right Alt|Both Alt|Either Alt
    Gui Add, DropDownList, x140 y105 w120 AltSubmit vKeybind_Key_Ctrl, No Ctrl||Left Ctrl|Right Ctrl|Both Ctrl|Either Ctrl

    Gui Add, DropDownList, x15 y150 w120 AltSubmit vKeybind_Option_Type gKeybind_Option_Type-Changed, Remap||Preset|Website|App|Custom
    Gui Add, DropDownList, x140 y150 w120 AltSubmit vKeybind_Option_Mode gKeybind_Option_Mode-Changed, Either Mode||Orange Mode|Blue Mode
    Gui Add, CheckBox, x265 y153 w120 vKeybind_Option_GoBlue, Go to Blue

    Gui Add, ComboBox, x15 y195 w180 h200 vKeybind_Option_Remap, Disable Keybind||LButton|RButton|MButton|XButton1|XButton2|WheelDown|WheelUp|WheelLeft|WheelRight|Space|Tab|Enter|Escape|Backspace|Delete|Home|End|PgUp|PgDn|Up|Down|Left|Right|PrintScreen|Numpad0|Numpad1|Numpad2|Numpad3|Numpad4|Numpad5|Numpad6|Numpad7|Numpad8|Numpad9|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12|F13|F14|F15|F16|F17|F18|F19|F20|F21|F22|F23|F24
    Gui Add, DropDownList, x15 y195 w180 AltSubmit vKeybind_Option_Preset, Restart Program||Toggle Emulated Numpad|Send Notification
    Gui Add, Edit, x15 y195 w180 r2 -WantReturn Wrap vKeybind_Option_Website, https://

    GuiControl, Hide, Keybind_Option_Remap
    GuiControl, Hide, Keybind_Option_Preset
    GuiControl, Hide, Keybind_Option_Website
    GuiControl, Hide, Keybind_Option_App ; Yet to be added
    GuiControl, Hide, Keybind_Option_Custom ; Yet to be added

    Gui Add, Button, x75 y260 w120 h30 gKeybindEdit_Save, &Save Keybind
    Gui Add, Button, x205 y260 w120 h30 gKeybindEdit_Cancel, &Cancel

    ;Gui Show, Center w400 h300
Gui Settings:New, -MinimizeBox -MaximizeBox, Orange Mode Settings
    Gui Settings:Default
    Gui Font, s10, Lexend
    Gui Margin, 15, 15

    Gui Add, Tab3, -Wrap x0 y5 w500 h395, General|AutoClicker|Keybinds|Text Shortcuts

    Gui Tab, 1, 1
        Gui Add, Button, -Wrap x190 y50 w120 gBuildRestart, Build and &Restart
    ;
    Gui Tab, 2, 1
    ;
    Gui Tab, 3, 1
        Gui Add, Button, -Wrap x55 y40 w120 h30 gKeybindMenu_Add, &Add Keybind
        Gui Add, Button, -Wrap Disabled x185 y40 w120 h30 gKeybindMenu_Edit vKeybindMenu_Edit, &Edit Keybind
        Gui Add, Button, -Wrap Disabled x315 y40 w120 h30 gKeybindMenu_Del vKeybindMenu_Del, &Delete Keybind

        Gui Add, ListView, -LV0x10 -Multi Checked x20 y80 w460 h300 AltSubmit gKeybindListAction,  |Keybind|Type|Action|Mode
        LV_ModifyCol(1, 25)
        LV_ModifyCol(2, 250)
        LV_ModifyCol(3, 100)
        LV_ModifyCol(4, 300)
        LV_ModifyCol(5, 125)
    ;
    LV_Add(Check, 1,"CapsLock+LCtrl+RCtrl+LShift+RShift+LAlt+RAlt+LWin+RWin+G", "Website", "https://verylong.websitename.com", "Orange; Go Blue") ; For testing purposes

    ;Gui Show, Center w500 h400
Return

However, when I open the menu, change something, then exit and click one of the two buttons, nothing happens - the input fields do not clear/reset.

How can I fix this problem?

Note: If you have an idea for a better way of doing this, let me know - I'm open to suggestions.


Solution

  • With the code you posted, two things can be observed:

    1. You are passing a blank variable to the function (which, from what can be understood, should be a string).

    2. To use the ternary operator in GuiControl, you need to enable expression mode in the field( % sign ).

      Gui Add, ComboBox, x230 y24 w140 h200 vKeybind_Key_Main, LButton|RButton|MButton|XButton1|XButton2|WheelDown|WheelUp|WheelLeft|WheelRight|Space|Tab|Enter|Escape|Backspace|Delete|Home|End|PgUp|PgDn|Up|Down|Left|Right|PrintScreen|Numpad0|Numpad1|Numpad2|Numpad3|Numpad4|Numpad5|Numpad6|Numpad7|Numpad8|Numpad9|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12|F13|F14|F15|F16|F17|F18|F19|F20|F21|F22|F23|F24
      Gui Add, DropDownList, x15 y70 w120 AltSubmit vKeybind_Key_Win, No Win||Left Win|Right Win|Both Win|Either Win
      Gui Add, CheckBox, x140 y73 w120 vKeybind_Key_CapsLock, CapsLock
      Gui Add, DropDownList, x265 y70 w120 AltSubmit vKeybind_Key_Shift, No Shift||Left Shift|Right Shift|Both Shift|Either Shift
      Gui Add, DropDownList, x15 y105 w120 AltSubmit vKeybind_Key_Alt, No Alt||Left Alt|Right Alt|Both Alt|Either Alt
      Gui Add, DropDownList, x140 y105 w120 AltSubmit vKeybind_Key_Ctrl, No Ctrl||Left Ctrl|Right Ctrl|Both Ctrl|Either Ctrl
      
      Gui Add, DropDownList, x15 y150 w120 AltSubmit vKeybind_Option_Type gKeybind_Option_Type-Changed, Remap||Preset|Website|App|Custom
      Gui Add, DropDownList, x140 y150 w120 AltSubmit vKeybind_Option_Mode gKeybind_Option_Mode-Changed, Either Mode||Orange Mode|Blue Mode
      Gui Add, CheckBox, x265 y153 w120 vKeybind_Option_GoBlue, Go to Blue
      
      Gui Add, ComboBox, x15 y195 w180 h200 vKeybind_Option_Remap, Disable Keybind||LButton|RButton|MButton|XButton1|XButton2|WheelDown|WheelUp|WheelLeft|WheelRight|Space|Tab|Enter|Escape|Backspace|Delete|Home|End|PgUp|PgDn|Up|Down|Left|Right|PrintScreen|Numpad0|Numpad1|Numpad2|Numpad3|Numpad4|Numpad5|Numpad6|Numpad7|Numpad8|Numpad9|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12|F13|F14|F15|F16|F17|F18|F19|F20|F21|F22|F23|F24
      Gui Add, DropDownList, x15 y195 w180 AltSubmit vKeybind_Option_Preset, Restart Program||Toggle Emulated Numpad|Send Notification
      Gui Add, Edit, x15 y195 w180 r2 -WantReturn Wrap vKeybind_Option_Website, https://
      
      GuiControl, Hide, Keybind_Option_Remap
      GuiControl, Hide, Keybind_Option_Preset
      GuiControl, Hide, Keybind_Option_Website
      GuiControl, Hide, Keybind_Option_App
      GuiControl, Hide, Keybind_Option_Custom
      ; The code creating the buttons
      Gui Add, Button, -Wrap x55 y40 w120 h30 gKeybindMenu_Add, &Add Keybind
      Gui Add, Button, -Wrap Disabled x185 y40 w120 h30 gKeybindMenu_Edit vKeybindMenu_Edit, &Edit Keybind
      Gui, Show
      Return
      ; The function with the code
      SetGui(Name, RowValue=0) {
      
          if (Name = "Keybind") {
              GuiControl, Choose, Keybind_Key_Main,% (RowValue=0 ? 0 : Keybind_Key_Main)
              GuiControl, Choose, Keybind_Key_Win,% (RowValue=0 ? 1 : Keybind_Key_Win)
              GuiControl,, Keybind_Key_CapsLock,% (RowValue=0 ? 0 : Keybind_Key_CapsLock)
              GuiControl, Choose, Keybind_Key_Shift,% (RowValue=0 ? 1 : Keybind_Key_Shift)
              GuiControl, Choose, Keybind_Key_Alt,% (RowValue=0 ? 1 : Keybind_Key_Alt)
              GuiControl, Choose, Keybind_Key_Ctrl,% (RowValue=0 ? 1 : Keybind_Key_Ctrl)
              GuiControl, Choose, Keybind_Option_Type,% (RowValue=0 ? 1 : Keybind_Option_Type)
              GuiControl, Choose, Keybind_Option_Mode,% (RowValue=0 ? 1 : Keybind_Option_Mode)
              GuiControl,, Keybind_Option_GoBlue,% (RowValue=0 ? 0 : Keybind_Option_GoBlue)
              GuiControl, Choose, Keybind_Option_Remap,% (RowValue=0 ? 1 : Keybind_Option_Remap)
              GuiControl, Choose, Keybind_Option_Preset,% (RowValue=0 ? 1 : Keybind_Option_Preset)
              GuiControl,, Keybind_Option_Website,% (RowValue=0 ? "https://" : Keybind_Option_Website)
          }
      }
      
      ; The two button subs running that function
      KeybindMenu_Add:
          SetGui("Keybind", 0) ; Clear all form boxes
          ; MsgBox % "Clicked Add"
          Gui Keybind:Show, Center w400 h300, Add Keybind ; Open the Keybind Edit GUI with a new keybind
      Return
      
      KeybindMenu_Edit:
          SetGui("Keybind", 0) ; Clear all form boxes
              Gui Keybind:Show, Center w400 h300, Edit Keybind ; Open the Keybind Edit GUI with the currently selected row
      Return
      
      Keybind_Option_Mode-Changed:    ; just to avoid errors
      Keybind_Option_Type-Changed:    ; just to avoid errors
              Return
      

    Running the code