Next: , Previous: Terminal Output, Up: Basic Input and Output


16.1.2 Terminal Input

Octave has three functions that make it easy to prompt users for input. The input and menu functions are normally used for managing an interactive dialog with a user, and the keyboard function is normally used for doing simple debugging.

— Built-in Function: input (prompt)
— Built-in Function: input (prompt, "s")

Print a prompt and wait for user input. For example,

          input ("Pick a number, any number! ")
     

prints the prompt

          Pick a number, any number!
     

and waits for the user to enter a value. The string entered by the user is evaluated as an expression, so it may be a literal constant, a variable name, or any other valid expression.

Currently, input only returns one value, regardless of the number of values produced by the evaluation of the expression.

If you are only interested in getting a literal string value, you can call input with the character string "s" as the second argument. This tells Octave to return the string entered by the user directly, without evaluating it first.

Because there may be output waiting to be displayed by the pager, it is a good idea to always call fflush (stdout) before calling input. This will ensure that all pending output is written to the screen before your prompt. See Input and Output.

— Function File: menu (title, opt1, ...)

Print a title string followed by a series of options. Each option will be printed along with a number. The return value is the number of the option selected by the user. This function is useful for interactive programs. There is no limit to the number of options that may be passed in, but it may be confusing to present more than will fit easily on one screen.

— Built-in Function: keyboard (prompt)

This function is normally used for simple debugging. When the keyboard function is executed, Octave prints a prompt and waits for user input. The input strings are then evaluated and the results are printed. This makes it possible to examine the values of variables within a function, and to assign new values to variables. No value is returned from the keyboard function, and it continues to prompt for input until the user types `quit', or `exit'.

If keyboard is invoked without any arguments, a default prompt of `debug> ' is used.

For both input and keyboard, the normal command line history and editing functions are available at the prompt.

Octave also has a function that makes it possible to get a single character from the keyboard without requiring the user to type a carriage return.

— Built-in Function: kbhit ()

Read a single keystroke from the keyboard. If called with one argument, don't wait for a keypress. For example,

          x = kbhit ();
     

will set x to the next character typed at the keyboard as soon as it is typed.

          x = kbhit (1);
     

identical to the above example, but don't wait for a keypress, returning the empty string if no key is available.