Rapid-Q Documentation by William Yu (c)1999 | Chapter 6 |
CLS - Clear console screen PRINT - Write output to screen (visible/offscreen) INPUT - Accept input from user PCOPY - Used to copy to and from the screen buffers LOCATE - Position cursor POS(0) - Get current cursor column CSRLIN - Get current cursor row COLOR - Change color attribute of text POKE - Put attribute/char on screen PEEK - Get attribute/char from screen SLEEP - Delay INKEY$ - Non-blocking keyboard checkThese functions mimic the ones you would see under QBasic. PEEK and POKE operate the same way, SLEEP is more precise than QBasic's (ie. you can specify a fraction of a second, like in PowerBASIC). Notice that you don't require any DEF SEG statements, since this is Windows.
PRINT "Hello World!"Compile and run, you've just created your first useless console application for Windows! Let's move on to something more interesting, copying to and from screen buffers.
CLS PRINT "This is page 1" PCOPY 0, 1 '-- Copy visible page 0, to off screen page 1 CLS PRINT "Press any key to see page 1" DO:LOOP UNTIL INKEY$<>"" PCOPY 1, 0 '-- Copy off screen page 1, to visible page 0As you can see, our visible page is always 0. Our off screen pages range from 1-7. PCOPY just copies the whole buffer and places it into another. You can even copy an off screen buffer to another off screen buffer like so:
PCOPY 1,5The contents in buffer 1, is now copied to buffer 5. There's nothing new introduced here that you wouldn't have found in QBasic. One new feature is that you can now write to any offscreen page:
PRINT #1, "Hi World!" '-- Write to page 1The above example will write the string "Hi World!" to the offscreen page 1. Make sure you use the pound sign '#' else it will be treated as a number. To see it, use PCOPY 1, 0.
POKE 0, ASC("A") POKE 1, 15If you run this code, you'll see a white letter 'A' displayed at the upperleft corner of your console. One thing you should note is that a "byte" on the screen contains a character and its attribute. All even numbered address contain the character, all odd numbered addresses contain the attribute. If you don't exactly know what an attribute is, it's just a combination of the foreground color and background color all in one byte (8-bits). The lower 4 bits is reserved for the foreground color, and the upper 4 bits is reserved for the background color. If you know anything about bits and bytes, you can easily calculate the attribute:
B? = &H07 '-- 0 = background, 7 = foregroundYes, you can have high intensity background colors. Blinking is not supported yet.
INPUT "What is your age? ", AgeA quoted string can follow an INPUT statement, then a comma or semi-colon, followed by a variable. The input statement will store the result in the variable, string or numeric. The INPUT statement will block until the user has hit the enter key. For non-blocking keyboard inputs, try using INKEY$. It's a bit confusing to use at first, but it allows for customized keyboard handling. Let's use the same example above to demonstrate how this is done using INKEY$. However, in this version, we can customize input and filter out non-numeric characters.
PRINT "What is your age? "; AGE$ = "" DO DO A$ = INKEY$ LOOP UNTIL LEN(A$) IF A$ = CHR$(13) THEN EXIT DO IF A$ >= "0" AND A$ <= "9" THEN PRINT A$;:AGE$=AGE$+A$ LOOP PRINT PRINT "You are "; AGE$; " years old."Notice that if you press a non-numeric key, that character is not echoed. This is because we've restricted input to characters 0..9 only. Although the example is much longer than the previous one, we have customized our input using INKEY$ and that provides so much more power than INPUT. Another advantage of using INKEY$ is that you can also trap extended keys.
$OPTION INKEY$ TRAPALL ' trap all keys including shift, alt, ctrl, caps/num/scroll lock ' and the PopupMenu key $OPTION INKEY$ DEFAULT ' traps all extended keys except the ones listed aboveIf you've used QBasic, you probably know the scan codes inside out, so all the virtual key codes that Rapid-Q dishes out is exactly the same scan codes you'd see in QBasic. The following code is an example of how to trap/parse extended keys, particularly the arrow keys:
DO DO A$ = INKEY$ LOOP UNTIL LEN(A$) IF LEN(A$) = 2 THEN SELECT CASE RIGHT$(A$, 1) CASE "P" PRINT "Down arrow key pressed." CASE "H" PRINT "Up arrow key pressed." CASE "M" PRINT "Right arrow key pressed." CASE "K" PRINT "Left arrow key pressed." CASE ELSE PRINT "Extended key "; A$ END SELECT END IF LOOP UNTIL A$ = CHR$(27)Even if you didn't know the scan codes, you can look at the output of the above example to figure them out.
Prev Chapter | Contents | Next Chapter |