Chapter 8: User Interaction
|
8.1 |
Introduction
What's user interaction? Imagine a Nintendo without a joystick, absolutely no user interaction. Most newbies don't know how to use those extended keys (arrow keys, function keys, etc.), but user interaction is what makes or breaks a program. Imagine playing Mortal Kombat with the keys IRTYDH on your keyboard. Enough said. |
8.2 |
The keyboard
I think most of you know about INPUT and LINE INPUT, which both prompt the user to type in some data. I won't cover these, they are in the help file anyway. The most common question is using INKEY$ and reading those extended keys. |
8.2 |
Why is INKEY$ so popular?
INKEY$ is pretty much all you'll need (and want to use) once you become proficient in BASIC. In fact, using INPUT and LINE INPUT are shunned upon. Anyway, let's not get into that, we usually use INKEY$ to read extended keys like left arrow, up arrow, etc. Newbies won't know what's the difference between extended keys and normal ASCII keys, so I'll explain it simply, extended keys require 2 bytes to store. The first byte is an identifier that signals an extended key was pressed. Known as a NULL byte.
DO
A$=INKEY$
IF A$=CHR$(0)+"K" THEN
PRINT "Left Arrow Key Pressed."
END IF
LOOP Many people complain about not knowing the correct code for an extended key, but try including a LOCATE 1,1:PRINT A$ statement to the above code. Fine, now what? Run the code and hold the up arrow key, you'll probably see the letter H popup. You've just discovered the code for the up arrow key. Repeat this for any extended key.
TheKey$ = CHR$(0) + "?"
^ fill in
|
8.3 |
Simultaneous keypress
Wow, now you're being ambitious, this requires a direct poll of the keyboard. When you press a key, that information is stored in a buffer, sequentially, but we don't care for sequential access, we want simultaneous access. This isn't possible with INKEY$, you'll need to use something else. You can find a few routines under Keyboards.abc to handle multiple keypresses. Obviously this is very tricky and confusing stuff, avoid it unless you really need it (like 2-player games). |
8.4 |
The joystick
The most popular for games, but some people don't understand it's also a CPU hog. If you're using QBasic's supported joystick routines, get ready to snooze and lose speed. There's probably a few joystick routines in Misc.abc that are much faster than QBasic's. Why do joysticks need calibration? It's a pain, no doubt, and most of the time, once is not enough. This is a DOS thing, under Windows it seems stable. Anyway, the STICK, STRIG commands are in the help file, so look them up, it's easy to follow. |
|
|