Rapid-Q Documentation by William Yu (c)1999 Глава 6

6. Создание консольного приложения (Writing Console Applications)

Создать консольное приложение также несложно, как и приложение Windows.
Просто задайте компилятору директиву $APPTYPE CONSOLE вместо $APPTYPE GUI. Как вы можете найти,
проще изучить основы  Rapid-Q (и вообще Basic), создавая простые консольные приложения. 
Также консоль может дать некоторые возможности для отладки
GUI приложений.
6.1 Обзор функций консоли. (Overview of console functions )
Консольное приложение это приложение имеющее текстовый интерфейс, а не графический, как обычные Windows формы.
Консольное приложение в RapidQ имеет один видимый экран и 7 невидимых экранных буферов.
Ниже приводится обзор консольных команд.

   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 check)

Эти команды аналогичны командам  QBasic. Для SLEEP можно задавать доли секунды.

 Notice that you don't require any DEF SEG statements, since this is Windows.

6.2 Hello world пример

В Rapid-Q, как и в большинстве BASIC вы можете создать hello world программу, при помощи одной строки

     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.

   PRINT "This is page 1"

   PCOPY 0, 1    '-- Copy visible page 0, to off screen page 1

   PRINT "Press any key to see page 1"


   PCOPY 1, 0    '-- Copy off screen page 1, to visible page 0
As 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,5
The 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 1
The 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.
LOCATE and COLOR have the same effect on the offscreen pages. You can also PEEK from and POKE to the offscreen pages, just supply an additional parameter, the first being the page number to write/read from.

6.3 Using PEEK and POKE
If you've never used PEEK and POKE before, it's not a big deal. In Rapid-Q, PEEK and POKE have the same functionality of QBasic (except for the additional feature to PEEK/POKE to any offscreen page). Addresses range from 0 to 3999. This is because the screen size is restricted to 80x25 in Rapid-Q. But if 80x25 = 2000, what's the extra 2000 for?
    POKE 0, ASC("A")
    POKE 1, 15
If 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 = foreground
Yes, you can have high intensity background colors. Blinking is not supported yet.

6.4 Accepting user input
You can accept user input via the INPUT statement or by the INKEY$ function. Rapid-Q's INPUT statement is not like "normal" Basic INPUT that you may be familiar with. It is more like a LINE INPUT in that it will prompt for a complete line of information and only terminates if the user hits the enter key.
   INPUT "What is your age? ", Age
A 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$ = ""
       A$ = INKEY$
     IF A$ = CHR$(13) THEN EXIT DO
     IF A$ >= "0" AND A$ <= "9" THEN PRINT A$;:AGE$=AGE$+A$
   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.

6.5 Trapping Extended Keys
Extended keys are 2 bytes in length, the first byte is an ESC character, which can be ignored, and the second byte is the virtual key code. In the Windows version, you can add the option to trap all keys, including shift, ctrl, key, caps lock, num lock, scroll lock, and that PopupMenu key (if you're using a Windows specific keyboard, no not the StartMenu Key, the other one, if it exists on your keyboard). You can turn on/off this option anytime you wish:

   ' trap all keys including shift, alt, ctrl, caps/num/scroll lock
   ' and the PopupMenu key


   ' traps all extended keys except the ones listed above
If 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:
       A$ = INKEY$

     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 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.

6.6 Mixing CONSOLE with GUI
As you've noticed in the above example, we've mixed some CONSOLE operations with GUI ones. This is perfectly valid as long as you've compiled your application as CONSOLE. Under Linux/Unix there are some strange differences, please refer to the compatibilty guide for more information. For example, a GUI application for Linux/Unix can perform some CONSOLE operations such as PRINT, INPUT, and CLS without being compiled as CONSOLE.

6.7 Linux/Unix Console
Since most terminals are different, some console features will not work. If your terminal does not support color, none will be displayed. You don't need to worry about these issues, as Rapid-Q performs all proper error checking for you. The same can be said for extended keys, some terminals don't support certain keys. However, most terminals do support the arrow keys and Page up/down keys. You're almost guaranteed that those keys will work for most terminals.

Prev Глава Содержание Next Глава