• Please review our updated Terms and Rules here

Choice equivalent in qbasic

Robuck

Experienced Member
Joined
Jul 30, 2017
Messages
65
Location
Philadelphia PA
Hello

I have been working a lot with qbasic on my 486 PC, and I am trying to make a roguelike game as practice. I can do most of the other stuff I need to code for the game, but my biggest problem right now is user input. Basically, I need there to be a way where you can press a key, and it will immediately return a variable and move on to the next portion of code. When I was doing some extremely simple batch coding, I would use the choice command, which works exactly the way i need it to in this situation. I have tried line input commands, but you have to press enter every time you put in the input. I have tried keydown commands, but I do not favor these because sometimes it's unresponsive, and you have to cycle through a portion of code over and over.

I would be greatly appreciative if someone told me the command im looking for, and a short example would be even better!

Thank you,
Mark Robuck
 
I seem to remember an "on key" command in qbasic or quick basic. Brain is all fuzzy though.

I have the Quick Basic 3.0 manual scanned so to speak. I just recently obtained QB 4.5 but that's not scanned. If you want the manual let me know. You can also get QB online sans manuals.

At some point you should look at say Quick C, or Quick Pascal / Turbo Pascal for that matter.
 
You could use INKEY$ to get the current key pressed (remember to save it in a variable, as it will empty the character from the input buffer on each call) and SELECT CASE to handle the various options. However, INKEY$ does not handle special keys (i.e. the arrow keys,) only key combinations that translate to ASCII characters.
 
Like @commodorejohn, I was thinking INKEY$ -- allows you to flush the input buffer then get codes for all keys except some specials (Ctrl-Break, PrtScr, etc.); extended codes are returned in two invocations.
 
I think INPUT$(1) is actually what OP wants. It will block until you press a key, but does not require enter like INPUT.

INKEY$ does work with arrow keys (returns CHR$(0) + "H", CHR$(0) + "P", CHR$(0) + "K", or CHR$(0) + "M"). It's best used for situations where you are tapping keys and not holding them down.

If you want to be able to hold down keys, it's better to read port 60h directly.
 
If you're really after something that works real-time--that is, waits for an actual keypress, then INKEY$ is probably part of the solution. The problem with INPUT$(1) is that you'll get any key that was pressed before you got to the function call--that is, it will be buffered by the keyboard BIOS.

INKEY$ will always return--if there's no key buffered, it will return an empty string. So you can empty the typeahead buffer and wait for a real-time keypress.

See here for details
 
All that is correct, but a roguelike is turn-based and not real time.
 
If a player hits a key twice then they move two spaces.
 
Correct. They are typically single player, and the game only updates one step after you move.

Personally I would use INKEY$ as well, because it's more flexible and it works with the arrow keys. But the original question posed was the QB equivalent of CHOICE, which is INPUT$(1). For a roguelike I think either will work.
 
Back
Top