Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Keyboard

QBasic Information Center Tutorials - Keyboard
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Keyboard (User Input)
QBasic provides very limited features for allowing a user to make an input to an application. Except for the event trapping features, the only mechanism for taking user input is via the keyboard using the following functions.

    • Keyboard    
     input, input$, line input
    • Keystroke
     inkey$, key, key(n), On Key ... 

Keyboard Functions Reference
Here's a quick reference of the available keyboard input functions. These functions can read from a keyboard or from an open file. See the section on files for using input function on files. The emphasis on this page is to cover using the input functions to get user input from the keyboard.

  • inkey$ - reads a character from the keyboard
        result$ = inkey$
        

    Whereas input$ waits for a user to input a key, the inkey$ function checks the key buffer. If the buffer is empty, a "" is returned and the QBasic program continues execution on the next line of code.

    When a user presses a key on the keyboard, the key goes into a buffer, awaiting processing by the next call of inkey$.

  • input - reads input from the keyboard or a file
        input ; "Prompt" ; var1$, var2, ...  # syntax
        input a$              # user enters value and presses "Enter"
        input ; "Enter:", a$, b$   
            # first ; leaves cursor on same line after pressing "Enter"
            # "Prompt" string is optional
            # second ; appends question mark to prompt string
            # user must type a comma to separate entires for a$ and b$
            # press "Enter" after entering all variables 
        

  • input$ - reads specified number of characters from keyboard
        result$ = input$(5)       # reads 5 characters from keyboard
                                  # screen does not echo the input
        
    Note: Once the required number of characters are entered, the QBasic program continues without the user having to press the "Enter" key.

  • key - assigns/display function key string values
        key 10, var$    # assign var$ content to F10
        key n%, var$    # see below for allowed n% values
        key list        # displays F1-F12 assignments
        key on          # turns on function-key display line
        key off         # turns off function-key display line
        

    Note: See the next entry for values of n%. The function-key display line only happens during execution of a program.

  • key(n) - enables/disables/suspends event trapping of a key
        On Key(n%) GoSub line   # run subroutine when key is pressed
        Key(n%) on              # enables event trapping for key n%
        Key(n%) off             # disables event trapping for key n%
        Key(n%) suspends        # suspends event trapping for key n% 
                                  (events are saved and processed later
                                  when event trapping is re-enabled)
        

    Key Values
    QBasic supports the following values for n%, covering the F1-F12 and arrow keys. Note that the values 15-25 are available for the user to define which keys or key combinations are trapped. See QBasic help for more details.

            n%        Key
            ------    --------------------------------------------
            0         All keys listed here (KEY(0) ON, KEY(0) OFF,
                      and KEY(0) STOP only).
            1-10      Function keys F1-F10.
            11        Up Arrow key.
            12        Left Arrow key.
            13        Right Arrow key.
            14        Down Arrow key.
            15-25     User-defined keys
            30, 31    Function keys F11 and F12.
        

  • line input - accepts keyboard input until the Enter key is pressed.
        result$ = line input ; "Prompt" ; var$    # one variable only
        result$ = line input a$   # just enter value and press "Enter"
        result$ = line input$     # reads 5 characters from keyboard
        

If you have any suggestions or corrections, please let me know.