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
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 figure 16.1
Clearing The Window To clear the console window use the command CLS. CLS removes all text the console window. OPENCONSOLE
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 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 |