Get a Line of User Input

All about writing scripts for Celestia in Lua and the .cel system
Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 21 years 4 months
Location: Colorado, USA (7000 ft)

Get a Line of User Input

Post #1by don » 19.04.2004, 08:39

This easy-to-use function allows the user to enter a single line of text in reply to your prompt for information. Get the file here: http://www.donandcarla.com/Celestia/celx_scripting/GetUserInput_v1-0.celx.txt

Check out my Celestia web page for more scripts, functions, helpful guides, and more (http://www.donandcarla.com/Celestia/).

Enjoy!

-Don G.

Code: Select all

--***************************************************************************
--                  Don G's Celx/Lua getUserInput Function                  *
--                              (version 1.0)                               *
--                                                                          *
--  This function allows the user to enter a single line of text in reply   *
--  to your prompt for information.                                         *
--                                                                          *
--***************************************************************************


--***************************************************************************
--                            Global Variables                              *
--***************************************************************************
  userKeypress = ""  -- Used with keyboard entry


--***************************************************************************
--                         Keyboard Input Callback                          *
--                                                                          *
-- This function is called automatically by Celestia when celestia:         *
-- requestkeyboard() is set to true.                                        *
--                                                                          *
--***************************************************************************
  function celestia_keyboard_callback(char)
    userKeypress = char
    return true  -- Tell Celestia we will handle this keypress
  end


--***************************************************************************
--                   getUserInput from the keyboard                         *
--                                                                          *
--  This function allows the user to enter a single line of text in reply   *
--  to your prompt for information ('prompt' is a string value).            *
--***************************************************************************
  function getUserInput(prompt)
    local inputLine = ""
   
    if prompt == nil then
       celestia:print("getUserInput Error: Please include prompt text.",
                       5, -1, -1, 1, 5)
       wait(5)
       return ""
    end
   
    origTimeScale = celestia:gettimescale()  -- Get the current time-scale
    celestia:settimescale(0)                 -- Pause time
    userKeypress = ""                        -- Clear the userKeypress var
    celestia:requestkeyboard(true)           -- Enable keyboard input

    while true do                            -- Loop until we get Enter key
      -- Display the prompt...
      celestia:print(prompt .. inputLine, 100, -1, -1, 1, 5)
      wait(0.01)

      -- What key did the user press...
      if userKeypress == "\013" then      -- Enter key, we're done
         break

      elseif userKeypress == "\008" then  -- Backspace key, remove last char
         local strlen = string.len(inputLine)
         if strlen <= 1 then
            inputLine = ""
         else
            inputLine = string.sub(inputLine, 1, strlen - 1)
         end

      else  -- Add the character to inputLine...
         inputLine = inputLine .. userKeypress

      end
      userKeypress = ""

    end

    celestia:requestkeyboard(false)       -- Disable keyboard input
    celestia:settimescale(origTimeScale)  -- Reset the time scale
    return inputLine
  end


--***************************************************************************
--                   EXAMPLE of how to use this function                    *
--***************************************************************************

-- Call the function. The text is used as the user prompt...
   userInput = getUserInput("Enter text (then press ENTER): ")

-- The user pressed the ENTER key, so let's see what we got...
   if userInput ~= "" then
      celestia:flash("Text entered: " .. userInput, 5)
      wait (5)
   else
      celestia:flash("No text was entered.", 5)
      wait (5)
   end

   celestia:flash("End of script.", 3)
   wait (3)
   

Return to “Scripting”