QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: 40wattstudio on March 16, 2020, 09:45:33 am
-
How I interpret the above: LOOP UNTIL [Capslock and (y or n)] or [y or n]]
Trying to get "y" or "n" input. Getting lowercase to work is easy peasy. But I can't figure out how to get _KEYDOWN to detect uppercase in the event that the user accidentally left caps lock turned on.
I've already looked at the Wiki but I'm not finding it very helpful in explaining my particular situation.
-
You need to test for the ASCII value of both upper and lower case.
IF _KEYDOWN(89) OR _KEYDOWN(121) THEN ' test for y or Y
IF _KEYDOWN(78) OR _KEYDOWN(110) THEN ' test for n or N
_KEYDOWN won't report the condition of caps lock, only that the key is down or not.
-
Thanks for the quick response Terry!
Your solution works . . . however, I noticed that if the user presses Caps Lock then suddenly _KEYDOWN will sometimes refuse to detect the keys, even if I change it back to its original setting.
Now obviously the player shouldn't have any good reason to be hitting the Caps Locks mid-game, but still trying to cover all my bases here in case it ever does happen.
-
I've never run across that scenario when using _KEYDOWN or _KEYHIT. Would you happen to have some example code I can plug in to see this behavior?
-
PRINT "PRESS ESCAPE TO EXIT" PRINT "press A and D keys to test"
So in the game, one presses the A and D keys to change weapons. Above is a simplified version of that. Now I noticed that the above program works flawlessly whether one starts in a CAPS OFF state or CAPS ON state. Switching between the two works as well.
Now in the game itself I've noticed this consistency: If I start the game with CAPS OFF then I can switch between A and D. I can even turns CAPS ON, switch between A and D and go back to CAPS OFF with A and D switching still working.
But, if I start the game with CAPS ON, A and D switching initially works, but as soon as I turn CAPS OFF, then nothing happens. If I revert back to CAPS ON nothing works there either.
Below is a very simplified version of my game code:
changeweapon
weapon.AUTOLASER = 1
weapon.SINGLELASER = 0
weapon.SINGLELASER = 1
weapon.AUTOLASER = 0
-
For y or n, yes or no, why use _KEYDOWN? that is best tool for a combination of keys.
-
For y or n, yes or no, why use _KEYDOWN? that is best tool for a combination of keys.
I don't remember the website (archive-something) but it had a bunch of QBasic games and for about every one of them the reviewer blasted those that used INKEY$. If _KEYDOWN is not the best way to be detecting key presses then what is?
-
Found the site:
http://games.phatcode.net/dumpbyinput.php?input=A/&mode=1
Here were a couple of the critiques:
"A Bomberman clone with ok gameplay, but poorly coded. The movement is jerky because the designer used the INKEY$ statement"
"horrible controls (INKEY$)"
-
If _KEYDOWN is not the best way to be detecting key presses then what is?
_KEYHIT (for letter keys) but don't understand problem with INKEY$ unless it was used poorly.
Now in game control when you do need a bunch of keys and key combinations then _KEYDOWN is your best friend with keys like Alt, Ctrl, Shift...
-
So I updated the code to use _KEYHIT and everything is working properly now, whether I start from a CAPS OFF state or CAPS ON.
Thanks for the help Terry and Bplus!