ListBox Enter pressed event?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
slishnevsky
Posts: 52
Joined: 07 Mar 2024, 06:50

ListBox Enter pressed event?

Post by slishnevsky » 15 Apr 2024, 13:31

Hello.

Is there an Enter pressed event for ListBox in AHK 2?

Let's say I selected an item in ListBox, and I want to submit it by pressing Enter key?

Is it possible, like using OnEvent() method for example?

Something like

Code: Select all

MyListBox.OnEvent('Enter', EnterPressed)
I know there is no "Enter" event for ListBox, so, what should I use instead?

"Change" event exists, but it doesn't capture Enter press.

Thanks

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 15 Apr 2024, 14:14

You can do it with an invisible button that is set to Default:

Code: Select all

#Requires AutoHotkey v2.0
MyGui := Gui()
MyGui.Add('ListBox', 'vColors', ['Red','Green','Blue'])
MyGui.Add('Button', 'y-30 Default').OnEvent('Click', EnterPressed)
MyGui.Show

EnterPressed(*) {
	MsgBox MyGui['Colors'].Text ' was chosen!'
}

XMCQCX
Posts: 287
Joined: 14 Oct 2020, 23:44

Re: ListBox Enter pressed event?

Post by XMCQCX » 16 Apr 2024, 14:38

Hotkeys can be assigned for when the GUI is active using HotIfWinActive.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

gMainTitle := 'GuiTitle - ScriptName'

HotIfWinActive(gMainTitle ' ahk_class AutoHotkeyGUI')
Hotkey('Enter', gMain_lb_Submit)

gMain := Gui(, gMainTitle)
gMain.lb := gMain.Add('ListBox', 'w300 h300', ['Red','Green','Blue'])
gMain.Show()

gMain_lb_Submit(*)
{
    try strTxt := ControlGetChoice(gMain.lb.hwnd, gMainTitle ' ahk_class AutoHotkeyGUI')
    catch
        return
   
    ToolTip('Submit: ' strTxt), SetTimer((*) => ToolTip(), -6000)   
}

ntepa
Posts: 480
Joined: 19 Oct 2022, 20:52

Re: ListBox Enter pressed event?

Post by ntepa » 16 Apr 2024, 16:12

Another option:

Code: Select all

#Requires AutoHotkey v2.0

MyGui := Gui()
MyListBox := MyGui.AddListBox(, ["red","green","blue"])
WM_KEYDOWN := 0x0100
OnMessage(WM_KEYDOWN, OnKeyDown)
MyGui.Show()

OnKeyDown(wParam, lParam, msg, hwnd) {
    key := GetKeyName(Format("VK{:x}", wParam))
    if hwnd = MyListBox.hwnd && key = "Enter" {
        MsgBox(MyListBox.Text " selected")
    }
}

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 08:47

Got same problem: i can't store my variable "Etichetta" out of ok_click funtion:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

Spessore := 1
Cliente := "Codice/Cliente"
Posiz := 1
Altezza := 10
Qta := 1
Mat := "Fe."
Qual := "S235JR"
Etichetta := ""

MyGui := Gui(,"Etichettatrice")
MyGui.Add("Text",, "Inserisci i dati per la compilazione Automatica       ")
MyGui.Add("Text",, "Spessore in mm",)
MyGui.Add("ComboBox", "vSpessore w150", ["1","1.5","2","2.5","3","4","5","6","8","10","12","15","20","25","30","35"])
MyGui.Add("Text",, "Cliente o Codice")
MyGui.AddEdit("vCliente w150",Cliente)
MyGui.Add("Text",, "Progressivo Posizione")
MyGui.AddEdit("vPosiz w150",Posiz)
MyGui.Add("Text",, "Altezza del testo")
MyGui.AddEdit("vAltezza w150",Altezza)
MyGui.Add("Text",, "Quantità")
MyGui.AddEdit("vQta w150",Qta)
MyGui.Add("Text",, "Materiale")
MyGui.Add("ComboBox", "vMat w150", ["Fe.","Hardox.","inox.","Domex.","Zincata.","Bugnata.","Striata.","Alu.","Ottone."])
MyGui.Add("Text",, "Qualità")
MyGui.Add("ComboBox","vQual w150",["S235JR","S275JR","S355J2","S235JR Decapata","H450","H500","Aisi304","Aisi304 Satinata","Aisi3016"])
Etichetta := MyGui.Add("Button", "Default w150", "OK").OnEvent("Click", OK_Click)
MyGui.Show
WinSetAlwaysOnTop "1", "Etichettatrice"
ControlChooseIndex "1", "ComboBox1","Etichettatrice"
ControlChooseIndex "1", "ComboBox2","Etichettatrice"
ControlChooseIndex "1", "ComboBox3","Etichettatrice"

OK_Click(*)
{
Salvati := MyGui.Submit()  ; Save the contents of named controls into an object
    MyGui.Hide()
	Etichetta := Salvati.Spessore . "mm-" . Salvati.Cliente . "." . Salvati.Posiz . "-" . Salvati.Qta . "pz " . Salvati.Qual
	tooltip Etichetta
	msgbox Etichetta
	Return Etichetta
}


F10::
{
MyGui.Show
}


F9::
{
MsgBox Etichetta
}
it work inside but when press F9 the variable is empty :( helpme


[Mod edit: Changed inline code tags (c) to [code][/code] tags that produce an AHK code box. Please use them yourself when posting code.]

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 17 Apr 2024, 09:23

It can't do the same things as the other function if you don't tell it to do those same things. Even easier, just call the other function:

Code: Select all

F9::OK_Click

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 09:47

ok but call the function not store my variable i need it to menage in other functions,
successive step is how to menage variables in the gui increase, subtract, change combobox etc etc by a key and store it to " send %etechetta% " like in v1 AutoHotKey .

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: ListBox Enter pressed event?

Post by boiler » 17 Apr 2024, 10:12

If you want what you assign inside the function (like the Etichetta variable) to be seen outside the function, then you need to declare it as global in the function and any other function in which you want to access it that assigns it a new value since it will assume it's local in those cases. In this version of the code, after you submit the GUI by clicking OK, the MsgBox will display the contents of Etichetta when you press F9 because it is declared global in the function that assigns its value.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

Spessore := 1
Cliente := "Codice/Cliente"
Posiz := 1
Altezza := 10
Qta := 1
Mat := "Fe."
Qual := "S235JR"
Etichetta := ""

MyGui := Gui(,"Etichettatrice")
MyGui.Add("Text",, "Inserisci i dati per la compilazione Automatica       ")
MyGui.Add("Text",, "Spessore in mm",)
MyGui.Add("ComboBox", "vSpessore w150", ["1","1.5","2","2.5","3","4","5","6","8","10","12","15","20","25","30","35"])
MyGui.Add("Text",, "Cliente o Codice")
MyGui.AddEdit("vCliente w150",Cliente)
MyGui.Add("Text",, "Progressivo Posizione")
MyGui.AddEdit("vPosiz w150",Posiz)
MyGui.Add("Text",, "Altezza del testo")
MyGui.AddEdit("vAltezza w150",Altezza)
MyGui.Add("Text",, "Quantità")
MyGui.AddEdit("vQta w150",Qta)
MyGui.Add("Text",, "Materiale")
MyGui.Add("ComboBox", "vMat w150", ["Fe.","Hardox.","inox.","Domex.","Zincata.","Bugnata.","Striata.","Alu.","Ottone."])
MyGui.Add("Text",, "Qualità")
MyGui.Add("ComboBox","vQual w150",["S235JR","S275JR","S355J2","S235JR Decapata","H450","H500","Aisi304","Aisi304 Satinata","Aisi3016"])
Etichetta := MyGui.Add("Button", "Default w150", "OK").OnEvent("Click", OK_Click)
MyGui.Show
WinSetAlwaysOnTop "1", "Etichettatrice"
ControlChooseIndex "1", "ComboBox1","Etichettatrice"
ControlChooseIndex "1", "ComboBox2","Etichettatrice"
ControlChooseIndex "1", "ComboBox3","Etichettatrice"

OK_Click(*)
{
Salvati := MyGui.Submit()  ; Save the contents of named controls into an object
    MyGui.Hide()
	global Etichetta := Salvati.Spessore . "mm-" . Salvati.Cliente . "." . Salvati.Posiz . "-" . Salvati.Qta . "pz " . Salvati.Qual
	tooltip Etichetta
	msgbox Etichetta
	Return Etichetta
}

F10::MyGui.Show

F9::MsgBox Etichetta

Danielsan73
Posts: 21
Joined: 07 Nov 2014, 10:20

Re: ListBox Enter pressed event?

Post by Danielsan73 » 17 Apr 2024, 10:56

Thankyou boiler

that i need ! solved

User avatar
xMaxrayx
Posts: 367
Joined: 06 Dec 2022, 02:56
Contact:

Re: ListBox Enter pressed event?

Post by xMaxrayx » 09 Jul 2024, 05:11

another method, if you made a class ,you can use anykey with #hotif winactive/winexit to get listbox.text value.

you need make two variable ,one variable for the GUI and the another variable for ListBox.

then you make like this

#hotif winactive(gui_Variable)
enter :: msgbox(listbox_variable.text)
esc ::gui_Variable.destroy()



this my script

Code: Select all

#Requires AutoHotkey >=2.1- <3.0
; #SingleInstance Force                                                                                                                                                                                           

#Include <maxray\others\Tebayaki\GetCaretPosEx> ;https://github.com/Tebayaki/AutoHotkeyScripts





inlineBox_object := inlineBox()  ;

a::
::/h::{
    inlineBox_object([
        "Welcome to the Customer Service. My name  is Alex the Dapper , the best dapping performance in the world. see you later."
        ,"Hi. How are you? fine thank you goodbye!"
        ,"hi five , how i can help you?"
        ,"Hi, im sleeping rn good bye, thanks for contacting best customer servers"
        ,1+1
        ,2+9/2*77
        ,FormatTime("dddd MMMM d, yyyy hh:mm:ss tt")
        ,"Time Is :`n" FormatTime("dddd MMMM d, yyyy hh:mm:ss tt")
        ,7777
        ,"i`ni`ni`n`ti"
        
        
    ]
    ,
    , "Choose1 W550" 
    
    )

}


#HotIf WinActive(inlineBox_object._inlineBox__GUI)

greetingSentence__Array := [
"howdy?" ,"Wasup?" ,"Hi","yoohoo"
,"greating?" ,"1011111010100101001010,"
,"#@@#$#@" , "💢💢💢" ,":3"
,"୧(๑•̀ᗝ•́)૭" , "⸜(。˃ ᵕ ˂ )⸝♡", "(˶ᵔ ᵕ ᵔ˶)"
]


F1::{
    MsgBox(
        ;//msg
        greetingSentence__Array[
            Random(1,greetingSentence__Array.Length)
        ]
        ",`n  you select :`n"
        inlineBox_object._List_inlineBox__Listbox.Text

        ,"Attention!"

        ,"0x40"
    )

}



F2::{
    MsgBox(
            inlineBox_object
                    ._List_inlineBox__Listbox
                        .Text
            )
}


F3::MsgBox(inlineBox_object._List_inlineBox__Listbox.Text)


BackSpace::
Esc::{
    inlineBox_object.Destroy()
}



class inlineBox {

    _inlineBox__GUI := 0
    _List_inlineBox__Listbox := 0
    _List__ArrayObject := 0
    _ListOption := ""
    ; _x := 0
    ; _y := 0


    Call(
    List__ArrayObject := [1,2,3,"777","car" , 9/3] 
    ,actionForExitedWindow := "Kill"
    ,ListOption := ""
    ,maxRow := 0
    ,autoShow := 1
    ){
  
        if (this._inlineBox__GUI??0){

            if(actionForExitedWindow  ~= "i)Kill"){
                this._inlineBox__GUI.Destroy()
                this._inlineBox__GUI := 0
                return
            }
            
            else if (actionForExitedWindow  ~= "i)move"){
                ;!?
            }
        } 

        {   ;//setter
        this._ListOption := ListOption
        this._List__ArrayObject := List__ArrayObject
        }


        this._inlineBox__GUI :=  Gui()
        this._inlineBox__GUI.Opt("-Caption +ToolWindow +AlwaysOnTop")
        this._inlineBox__GUI.MarginX := 0
        this._inlineBox__GUI.MarginY := 0
        

        
        if maxRow == 0{
            maxRow := this._List__ArrayObject.Length
            maxRow := " r" maxRow
        }
        

        this._List_inlineBox__Listbox := this._inlineBox__GUI.AddListBox( this._ListOption . maxRow ,this._List__ArrayObject)
        this._List_inlineBox__Listbox.OnEvent("DoubleClick" , _List_inlineBox__Listbox___DoubleClick)

        this._inlineBox__GUI.Add('Button', 'y-30 x-20 Default')
        .OnEvent('Click', secretButton___Click)
        
        secretButton___Click(*) {    
            A_Clipboard := (this._List_inlineBox__Listbox.text)
            this.Destroy()
            Sleep(50)
            SendInput("^v")

        }

        
        if autoShow??1 == 1{
            this.Show()
        }

        _List_inlineBox__Listbox___DoubleClick(ListBox,*){
            A_Clipboard := (this._List_inlineBox__Listbox.text)
            this.Destroy()

        }



    }

    Destroy(){
        this._inlineBox__GUI.Destroy()
        this._inlineBox__GUI:=0
    }
    
    Show(){



        GetCaretPosEx(&x ,,,&y )
        if !(x??0){
            MouseGetPos(&x)
        }

        if !(y??0){
            MouseGetPos(,&y)
        }
        this._inlineBox__GUI.Show("x" x " y" y " AutoSize")

    }



    
}
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Post Reply

Return to “Ask for Help (v2)”