none
Up and Down RRS feed

  • Question

  •   TextWindow.Title = "Road Kill Cafe"
      space()
      TextWindow.WriteLine("1. Main Steet Entrees")
      TextWindow.WriteLine("2. A Taste of the Wild Side")
      TextWindow.WriteLine("3. Eating More Food is Fun")
      TextWindow.WriteLine("4. Late Night Delights")
      TextWindow.WriteLine("5. Roadkill Cafe Menu Challengers")

    Instead of pressing 1 2 3 4 5 or 6 I want to be able to press up or down and press enter.

    Monday, November 17, 2014 10:01 PM

Answers

  • Not simply without using an extension to grab keystrokes to move cursor in TextWindow.

    Options (I would choose Option 1):

    1] Do it in GraphicsWindow where KeyDown and KeyUp events can be used.

    2] Do it with pressing 1, 2, 3 et.

    3] Use extension - if you want to use LitDev extension then this can be done (otherwise the options above), example using extension below, this is a bit more advanced for several reasons, mainly about handling events (also arrays):

    'Create the options, indexed from zero to match TextWindow rows
    TextWindow.Title = "Road Kill Cafe"
    data[0] = "Main Steet Entrees"
    data[1] = "A Taste of the Wild Side"
    data[2] = "Eating More Food is Fun"
    data[3] = "Late Night Delights"
    data[4] = "Roadkill Cafe Menu Challengers"
    numData = Array.GetItemCount(data)
    minRow = 0 'Indexed from 0
    maxRow = numData-1
    row = minRow
    TextWindow.Clear()
    SetOptions()
    
    selectionMade = "False"
    LDTextWindow.KeyDown = OnKeyDown
    
    'Keep looping waiting for selection to be made - also this prevents a key from closing TW and ending program
    While ("True")
      If (selectionMade) Then
        TextWindow.Clear()
        TextWindow.ForegroundColor = "Gray"
        TextWindow.Write("You selected : ")
        TextWindow.ForegroundColor = "Red"
        TextWindow.WriteLine(data[row])
        selectionMade = "False"
      EndIf
      Program.Delay(20) 'Don't mash cpu in infinite loop
    EndWhile
    
    'When a key is pressed, move up or down and redraw list highlighting new row
    'Set a flag when return is pressed, selecting a row
    Sub OnKeyDown
      key = LDTextWindow.LastKey
      If (key = "Down" And row < maxRow) Then
        row = row + 1
        SetOptions()
      ElseIf (key = "Up" And row > minRow) Then
        row = row - 1
        SetOptions()
      ElseIf (key = "Return") Then
        selectionMade = "True"
      EndIf
    EndSub
    
    'Draw the current options, highligthing current row in Red
    Sub SetOptions
      TextWindow.CursorTop = 0
      For i = minRow To maxRow
        If (i = row) Then
          TextWindow.ForegroundColor = "Red"
        Else
          TextWindow.ForegroundColor = "Gray"
        EndIf
        TextWindow.WriteLine(data[i])
      EndFor
      TextWindow.CursorTop = row
    EndSub
    • Edited by litdevModerator Monday, November 17, 2014 11:05 PM
    • Marked as answer by Dinomite07 Monday, November 17, 2014 11:16 PM
    Monday, November 17, 2014 11:00 PM
    Moderator