Sign in to follow this  
Followers 0
rackajuh

Input box key press event

3 posts in this topic

I made a gui, wich has a input box, and a list where i can find all the commands which are availabe.

It would be good if on every key press the list is refiltered by the first n characters what i already typed into the input box.

i found this which helped me a lot, but i have few problems aboute these:

I wanna detect the enter key, and when enter is pressed the _MenuInputExec() function would be executed

Now this is work but when i type in forexample alert and a space, and i click on the alert below the list, the fnction is executed, but all i wanted to replace the input box without execute

and my second problem that i found out that there is an advanced way to create controlls with GUIRegisterMsg(). can anybody help me how dowes it works? i just wanna understand.

my source:

#include <Array.au3>
#include <GuiConstants.au3>

Global $commands_array1[1]
Global $commands_array2[1]
Global $commands_array_length = 0



Global $MenuGui
Global $MenuList
Global $MenuInput


func AddMenuCommand($name,$exec)
    _ArrayAdd($commands_array1,$name)
    _ArrayAdd($commands_array2,$exec)
    if($commands_array_length==0 )then
        _arraydelete($commands_array1,0)
        _arraydelete($commands_array2,0)
    endif
    $commands_array_length += 1
endfunc

func OpenMenu()
    $MenuGui = GUICreate("Enter Command", 400, 100)
    $MenuInput = GUICtrlCreateInput("",0,0,400,20)
    $MenuList = GUICtrlCreateList("",0,20,400,80)
    for $i = 0 to $commands_array_length-1 step 1
        GuiCtrlSetData($MenuList,$commands_array1[$i])
    next
    
    GUIRegisterMsg(0x0111, "_MenuInputonkeydown")
    GUISetState(@SW_SHOW)

    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $MenuInput
                GuiSetState(@SW_HIDE)
                _MenuInputExec()
                exit
                
            Case $msg = $MenuList
                _MenuListClicked()
                GuiSetState(@SW_SHOW)
        EndSelect
    WEnd
endfunc

Func _MenuInputonkeydown($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF)
    Local $hCtrl = $lParam
    
    if($nID==$MenuInput and $nNotifyCode == 0x300) then
        _MenuInputKeypress()
    endif

    Return $GUI_RUNDEFMSG
EndFunc

func _MenuInputKeyPress()
    $param_all = GUICtrlRead($MenuInput)
    $param_array = StringSplit($param_all," ")
    
    $command = $param_array[1]
    
    
    GUICtrlSetData($MenuList,"")
    For $i=0 To $commands_array_length-1 Step 1
        if(StringCompare($command,StringLeft($commands_array1[$i],StringLen($command)))==0) then
            GuiCtrlSetData($MenuList,$commands_array1[$i])
        endif
    Next
endfunc

func _MenuInputExec()
    $param_all = GUICtrlRead($MenuInput)
    $param_array = StringSplit($param_all," ")
    
    $command = $param_array[1]
    if($param_array[0] > 1) Then
        $exec_param = $param_array[2]
    Else
        $exec_param = ""
    endif
    
    
    For $i=0 To $commands_array_length-1 Step 1
        if(StringCompare($command,$commands_array1[$i])==0) then
            Execute($commands_array2[$i] & '("'& $exec_param &'")')
        endif
    Next
endfunc

func _MenuListClicked()
    GUICtrlSetData($MenuInput,StringUpper(GUICtrlRead($MenuList)))
    ;this calls an event which is the same as perssing enter sometimes. for example type alert and a space, and then click on this list item (ALERT)
endfunc

func alert($text)   
    msgbox(0,"alert",$text)
endfunc

AddMenuCommand("ALERT","alert")

OpenMenu()

Share this post


Link to post
Share on other sites



rackajuh,

Welcome to the AutoIt forum. :P

This is how I implemented a list which altered the values to match the input - I hope it proves useful to you:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData, $asKeyWords[100]

; Create list full of random 4 character "words"
Keywords()

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
; Create dummy keys for accelerators
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)
; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($asKeyWords, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP ; Move up and down list
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN ; Move up and down list
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
                MsgBox(0, "Result", $sText)
            EndIf
            
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To 99
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

; This function is just to fill the list with random "words"
Func Keywords()

    Local $sData
    For $i = 0 To 99
        $asKeyWords[$i] = Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1))
        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

EndFunc   ;==>Keywords

i found out that there is an advanced way to create controlls with GUIRegisterMsg()

Where did you find this? :x If you want to know what GUIRegisterMsg actually does, I suggest reading the GUIRegisterMsg tutorial in the Wiki - it certainly does not create controls! :shifty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hey, that is exactly what I needed in my current project! Thanks for sharing, Melba.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0