The Console Window

Top  Previous  Next

The console window, also called a text console, is similar to a command prompt display. The console window can be used for text input and output only.  Graphics are not supported for console mode programs.

To open a console window in your program use the OPENCONSOLE command.   When you are done with the console window issue a CLOSECONSOLE command.    OPENCONSOLE and CLOSECONSOLE are not needed if you compile for a Console target but it does not hurt to included them.

If compiling for a Windows target you must use OPENCONSOLE or the program will lock trying to output or input to a non existent text console. Window only allows one console window per process.

Displaying Text

To display text in the console window use the PRINT statement.   The print statement has the following syntax.

PRINT {window,}param1{,param2...}{,}

The first optional parameter is to output text in a regular window and will be discussed in the section on using windows.

The optional trailing comma tells PRINT not to issue a carriage return at the end of the line.   This allows using multiple PRINT statements to work with the same line of text.

A print statement with no parameters will move the console window cursor to the next line. The following program demonstrates a simple console program.
 

OPENCONSOLE
DEF name$,address$,city$,state$,zip$:STRING
DEF income:FLOAT
name$ = "John Doe"
address$ = "123 Anywhere St"
city$ = "Middle Town"
state$ = "WI"
zip$ = "55555"
income = 38.5
PRINT name$
PRINT address$
PRINT city$," ",state$," ",zip$
PRINT
PRINT "Median Income:",
PRINT income
PRINT
PRINT "Press Any Key To Close"
DO
UNTIL INKEY$ <> ""
CLOSECONSOLE
END

 

Changing Colors

To display your text in different colors in the console window use the COLOR statement.   The COLOR statement has the syntax of:

COLOR foreground, background

Where background and foreground are positive numbers from 0 to 15.  Refer to figure 16.1 for the colors supported by the console window.

In the previous example, insert the statement:
 

COLOR 14,1

Right before all the PRINT statements to see yellow text on a blue background.

 

Positioning Text

You can position your text anywhere in the console window by using the LOCATE statement.   The LOCATE statement has the syntax of:

LOCATE y, x

where y is the vertical character position and x is the horizontal character position.   The origin is the upper left corner at character position 1,1.  This short example demonstrates the LOCATE statement:
 

OPENCONSOLE
LOCATE 10,1
PRINT "Position 10,1"
LOCATE 10,50
PRINT "Position 10,50"
LOCATE 1,1
PRINT "Position 1,1"
LOCATE 1,50
PRINT "Position 1,50"
LOCATE 12,1
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

figure 16.1

COLOR number

Color Produced

0

BLACK

1

BLUE

2

GREEN

3

CYAN

4

RED

5

MAGENTA

6

BROWN

7

WHITE

8

GRAY

9

LIGHT BLUE

10

LIGHT GREEN

11

LIGHT CYAN

12

LIGHT RED

13

LIGHT MAGENTA

14

YELLOW

15

HIGH INTENSITY WHITE

 

Clearing The Window

To clear the console window use the command CLS. CLS removes all text the console window.
 

OPENCONSOLE
PRINT "CCCCCCCCCCCCCCCCCCCC"
PRINT "XXXXXXXXXXXXXXXXXXX"
PRINT "Press any key to clear this window"
DO:UNTIL INKEY$ <> ""
CLS
LOCATE 12,1
PRINT"Now press any key to close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

 

Getting Input

So far, we have concentrated on displaying and manipulating text in the console window.  For a program to be interactive, we need some way to get data from the user into our program.  EBASIC provides one statement for the console window INPUT and one function  INKEY$.

The INPUT statement allows your program to receive information from the user directly into any assignable variable type.   The syntax of the INPUT statement looks like this:

INPUT {”prompt”,}variable

The first optional parameter is a literal string used as the prompt for the input statement.   The INPUT statement keeps collecting user key presses until the <ENTER> key is pressed at which time the input is stored in the variable.
 

OPENCONSOLE
CLS
DEF name$:STRING
INPUT "Enter your name:",name$
PRINT "Hello ",name$," Nice to meet you"
PRINT name$,"Please press any key to close!"
DOUNTIL INKEY$ <> ""
CLOSECONSOLE
END

The INKEY$ function waits for the user of your program to press one key and returns the character pressed. This return value can be assigned to either a STRING or CHAR variable. The syntax of the INKEY$ function is:

STRING|CHAR = INKEY$ { ( raw ) }

The optional raw parameter if equal to 1 tells INKEY$ to return virtual key codes. It is important to note that before your program executes the INKEY$ function that the user of your program may have typed a number of keys. Always check the return value for the expected results.

Example:

OPENCONSOLE
DEF key$:STRING
LOCATE 1,1
PRINT "INKEY$ demonstration. ",
PRINT "Press <ENTER> to end program"
PRINT "Press any other keys to display them"
DO
    key$ = INKEY$
    IF Key$ <> ""
       LOCATE 10,1
       COLOR 14,1
       PRINT "You Pressed >",
       COLOR 2,0
       PRINT key$,
    ENDIF
UNTIL key$ = CHR$(13)
CLOSECONSOLE
END