Jump to content

History in InputBox


Recommended Posts

I am trying to make a "command prompt" type input box.  What i mean by this, is when you press the "UP" arrow it will go up in your previously entered history, and "DOWN" will go back down through the history just like command prompt does.  Any suggestions on how to go about this?  This is what i have gotten so far, but not sure if HotKeys are the way to go.  Any input is greatly appreciated!

Thanks!

#include <GUIConstants.au3>

GUICreate("test", 200, 100)
$input = GUICtrlCreateInput("", 10, 10, 150, 20, $ES_CENTER)
GUICtrlSetLimit(-1, 4)
$input2 = GUICtrlCreateInput("", 10, 50, 150)
GUISetState()
HotKeySet("{DOWN}", "_HistoryDown")
HotKeySet("{UP}", "_HistoryUp")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If StringLen(GUICtrlRead($input)) = 4 Then
        Enter()
    EndIf
WEnd

Func Enter()
    GUICtrlSetData($input2, "This is the current input: " & GUICtrlRead($input))
EndFunc

Func _HistoryUp()
    GUICtrlSetData($input, "go up in history")
EndFunc

Func _HistoryDown()
    GUICtrlSetData($input, "go down in history")
EndFunc
Edited by hiimjoey11
Link to comment
Share on other sites

You'll need to create an array to store all the old strings. You can start at [1] with $i = 1 like so $array[$i] and increment every time you store a string ($i += 1).

You'll then need to make a GUI Accelerator for the Up array. Click the function name in the syn box to go to the helpfile, which includes a detailed example:

GUISetAccelerators

Try it, if you have any questions please post them. :)

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

You'll need to create an array to store all the old strings. You can start at [1] with $i = 1 like so $array[$i] and increment every time you store a string ($i += 1).

You'll then need to make a GUI Accelerator for the Up array. Click the function name in the syn box to go to the helpfile, which includes a detailed example:

GUISetAccelerators

Try it, if you have any questions please post them. :)

 

I think im getting closer... what am i doing wrong here..

#include <GUIConstants.au3>
#include <Array.au3>

GUICreate("test", 200, 200)
$input = GUICtrlCreateInput("", 10, 10, 150, 20, $ES_CENTER)
GUICtrlSetLimit(-1, 4)
$input2 = GUICtrlCreateInput("", 10, 50, 150, 100, $ES_MULTILINE)
GUISetState()
;HotKeySet("{DOWN}", "_HistoryDown")
;HotKeySet("{UP}", "_HistoryUp")
$HistDown = GUICtrlCreateDummy()
$HistUp = GUICtrlCreateDummy()
$Enter = GUICtrlCreateDummy()
Local $aAccelKeys[3][3] = [["{DOWN}", $HistDown], ["{UP}", $HistUp], ["{ENTER}", $Enter]]
GUISetAccelerators($aAccelKeys)
$arrayCount = 1
Dim $historyArray = []


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $HistDown
            If $arrayCount < 0 Then
                $arrayCount -= 1
                GUICtrlSetData($input, $historyArray[$arrayCount])
                Enter()
            Else
                GUICtrlSetData($input, "")
            EndIf

        Case $HistUp
            $arrayCount += 1
            GUICtrlSetData($input, $historyArray[$arrayCount])
            Enter()
        Case $Enter
            Enter()
    EndSwitch
WEnd

Func Enter()
    _ArrayAdd($historyArray, GUICtrlRead($input))
    GUICtrlSetData($input2, GUICtrlRead($input2) & @CRLF & GUICtrlRead($input))
EndFunc   ;==>Enter
Edited by hiimjoey11
Link to comment
Share on other sites

Here is something I whipped up for you:

#include <GUIConstants.au3>
#include <Array.au3>

Local $aHistory[15], $counter = 0, $hist_Counter = 0

HotKeySet("{DOWN}", "histDown")
HotKeySet("{UP}", "histUp")

GUICreate("test", 200, 100)
$input = GUICtrlCreateInput("", 10, 10, 150, 20, $ES_CENTER)
GUICtrlSetLimit(-1, 4)
$input2 = GUICtrlCreateInput("", 10, 50, 150)
GUISetState()
Local $enter = GUICtrlCreateDummy()
Local $aAccelKeys[1][2] = [["{ENTER}", $enter]]
GUISetAccelerators($aAccelKeys)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $enter
            addValue(GUICtrlRead($input))
            GUICtrlSetData($input2, "You set:" & GUICtrlRead($input))
            GUICtrlSetData($input, "")
    EndSwitch
WEnd

Func histDown()
    If $counter = -1 Then Return
    If $counter = 15 Then Return
    If $hist_Counter = 15 Then Return
    If $hist_Counter = -1 Then
        GUICtrlSetData($input, "")
        Return
    EndIf
    GUICtrlSetData($input, $aHistory[$hist_Counter])
    $hist_Counter -= 1
EndFunc   ;==>histDown

Func histUp()
    If $counter = -1 Then Return
    If $counter = 15 Then Return
    If $hist_Counter = 15 Then Return
    GUICtrlSetData($input, $aHistory[$hist_Counter])
    $hist_Counter += 1
EndFunc   ;==>histUp

Func addValue($value)
    If $counter = 0 Then
        $aHistory[$counter] = $value
        $counter = 1
        Return
    ElseIf $counter = 15 Then
        _ArrayDelete($aHistory, "0-15")
        $counter = 0
        $hist_Counter = 0
    EndIf
    $aHistory[$counter] = $value
    $counter += 1
EndFunc   ;==>addValue

That should give you a firm foundation to stand on. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Here is something I whipped up for you:

#include <GUIConstants.au3>
#include <Array.au3>

Local $aHistory[15], $counter = 0, $hist_Counter = 0

HotKeySet("{DOWN}", "histDown")
HotKeySet("{UP}", "histUp")

GUICreate("test", 200, 100)
$input = GUICtrlCreateInput("", 10, 10, 150, 20, $ES_CENTER)
GUICtrlSetLimit(-1, 4)
$input2 = GUICtrlCreateInput("", 10, 50, 150)
GUISetState()
Local $enter = GUICtrlCreateDummy()
Local $aAccelKeys[1][2] = [["{ENTER}", $enter]]
GUISetAccelerators($aAccelKeys)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $enter
            addValue(GUICtrlRead($input))
            GUICtrlSetData($input2, "You set:" & GUICtrlRead($input))
            GUICtrlSetData($input, "")
    EndSwitch
WEnd

Func histDown()
    If $counter = -1 Then Return
    If $counter = 15 Then Return
    If $hist_Counter = 15 Then Return
    If $hist_Counter = -1 Then
        GUICtrlSetData($input, "")
        Return
    EndIf
    GUICtrlSetData($input, $aHistory[$hist_Counter])
    $hist_Counter -= 1
EndFunc   ;==>histDown

Func histUp()
    If $counter = -1 Then Return
    If $counter = 15 Then Return
    If $hist_Counter = 15 Then Return
    GUICtrlSetData($input, $aHistory[$hist_Counter])
    $hist_Counter += 1
EndFunc   ;==>histUp

Func addValue($value)
    If $counter = 0 Then
        $aHistory[$counter] = $value
        $counter = 1
        Return
    ElseIf $counter = 15 Then
        _ArrayDelete($aHistory, "0-15")
        $counter = 0
        $hist_Counter = 0
    EndIf
    $aHistory[$counter] = $value
    $counter += 1
EndFunc   ;==>addValue

That should give you a firm foundation to stand on. :)

 

Works well except i think it's backwards! If you type in 1 (enter), 2 (enter), 3 (enter) and press the UP button it should show your last entry which is 3.. instead it shows your first entry.

Also, I just saw there is an _ArrayPop and _ArrayPush, i kind of got it working using them, but it's not right.  Any suggestions on this method?  

#include <GUIConstants.au3>
#include <Array.au3>

GUICreate("test", 200, 400)
$input = GUICtrlCreateInput("", 10, 10, 150, 20, $ES_CENTER)
GUICtrlSetLimit(-1, 4)
$input2 = GUICtrlCreateInput("", 10, 50, 150, 300, $ES_MULTILINE)
GUISetState()
;HotKeySet("{DOWN}", "_HistoryDown")
;HotKeySet("{UP}", "_HistoryUp")
$HistDown = GUICtrlCreateDummy()
$HistUp = GUICtrlCreateDummy()
$Enter = GUICtrlCreateDummy()
Local $aAccelKeys[3][3] = [["{DOWN}", $HistDown], ["{UP}", $HistUp], ["{ENTER}", $Enter]]
GUISetAccelerators($aAccelKeys)
$arrayCount = 1
Dim $historyArray = []


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $HistDown
            GUICtrlSetData($input, "")
            Enter()
        Case $HistUp
            $pop = _ArrayPop($historyArray)
            GUICtrlSetData($input, $pop)
            ;_ArrayPush($historyArray, $pop, 1)
        Case $Enter
            Enter()
    EndSwitch
WEnd

Func Enter()
    If Not GUICtrlRead($input) = "" Then
        _ArrayInsert($historyArray, UBound($historyArray) - 1, GUICtrlRead($input))
        GUICtrlSetData($input2, _ArrayToString($historyArray, @CRLF))
    EndIf
EndFunc   ;==>Enter
Link to comment
Share on other sites

 

Works well except i think it's backwards!

You are right, reverse the counters ($hist_Counter) and you should be good. Like a said. Just a foundation for you to build upon. You'll just need for loop to go through the values backwards from 15 to 0, looking to see if there was a value, that way it will go 3, 2, 1 instead of 1 enter 2 enter 3 enter- 1, 2, 3 returned.

There is certainly different ways of accomplishing this goal.

If I have some free time here soon (work time), I will work on the order.

I hope that helps you on your way though. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

You are right, reverse the counters ($hist_Counter) and you should be good. Like a said. Just a foundation for you to build upon. You'll just need for loop to go through the values backwards from 15 to 0, looking to see if there was a value, that way it will go 3, 2, 1 instead of 1 enter 2 enter 3 enter- 1, 2, 3 returned.

There is certainly different ways of accomplishing this goal.

If I have some free time here soon (work time), I will work on the order.

I hope that helps you on your way though. :)

 

Reverse it where exactly  :ermm:  But yes, this does help tremendously! Thanks for your time  :thumbsup:

Link to comment
Share on other sites

As you are using an Input in a GUI, are you aware, that you could also use a Combo instead, which you could populate with previous entries?

Often with a Combo, when you begin typing, certain entries will come to the fore when you click the down icon. Based on alphabetical from left.

This could possibly be a suitable solution for you?

(it gives a browse for right solution option as well)

P.S. Obviously MikahS has given you great help/solution already, but I offer an alternative you or others might not have thought of.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...