Author Topic: _KEYDOWN input regardless of case?  (Read 2290 times)

0 Members and 1 Guest are viewing this topic.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
_KEYDOWN input regardless of case?
« on: March 16, 2020, 09:45:33 am »
Code: QB64: [Select]
  1. LOOP UNTIL (_KEYDOWN(100301) AND (_KEYDOWN(121) OR _KEYDOWN(110))) OR (_KEYDOWN(121) OR _KEYDOWN(110)) 'Y OR N  
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.


Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
Re: _KEYDOWN input regardless of case?
« Reply #1 on: March 16, 2020, 09:57:55 am »
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.
In order to understand recursion, one must first understand recursion.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: _KEYDOWN input regardless of case?
« Reply #2 on: March 16, 2020, 10:14:35 am »
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.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
Re: _KEYDOWN input regardless of case?
« Reply #3 on: March 16, 2020, 10:19:53 am »
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?
In order to understand recursion, one must first understand recursion.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: _KEYDOWN input regardless of case?
« Reply #4 on: March 16, 2020, 10:56:23 am »
Code: QB64: [Select]
  1. LOCATE 1, 1
  2. PRINT "PRESS ESCAPE TO EXIT"
  3. PRINT "press A and D keys to test"
  4.  
  5.     IF _KEYDOWN(97) THEN
  6.         PRINT "a WAS PRESSED"
  7.     END IF
  8.  
  9.     IF _KEYDOWN(65) THEN
  10.         PRINT "A WAS PRESSED"
  11.     END IF
  12.  
  13.     IF _KEYDOWN(100) THEN
  14.         PRINT "d WAS PRESSED"
  15.     END IF
  16.  
  17.     IF _KEYDOWN(68) THEN
  18.         PRINT "D WAS PRESSED"
  19.     END IF
  20.  

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:

Code: QB64: [Select]
  1. DO ' the main loop
  2.  
  3. IF [condition] THEN
  4. changeweapon
  5.  
  6. LOOP UNTIL (_KEYDOWN(89) OR _KEYDOWN(121)) OR (_KEYDOWN(78) OR _KEYDOWN(110)) 'Y OR N
  7.  
  8. sub changeweapon
  9. IF (_KEYDOWN(100) OR _KEYDOWN(68)) AND weapon.SINGLELASER = 1 THEN ' - activates autolaser if D
  10.         weapon.AUTOLASER = 1
  11.         weapon.SINGLELASER = 0
  12.     END IF
  13.  
  14.     IF (_KEYDOWN(97) OR _KEYDOWN(65)) AND weapon.AUTOLASER = 1 THEN ' - activates singlelaser if a
  15.         weapon.SINGLELASER = 1
  16.         weapon.AUTOLASER = 0
  17.     END IF
  18. end changeweapon
  19.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _KEYDOWN input regardless of case?
« Reply #5 on: March 16, 2020, 10:59:55 am »
For y or n, yes or no, why use _KEYDOWN? that is best tool for a combination of keys.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: _KEYDOWN input regardless of case?
« Reply #6 on: March 16, 2020, 11:09:09 am »
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?

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: _KEYDOWN input regardless of case?
« Reply #7 on: March 16, 2020, 11:15:23 am »
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$)"

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _KEYDOWN input regardless of case?
« Reply #8 on: March 16, 2020, 11:24:55 am »
Quote
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...

« Last Edit: March 16, 2020, 11:34:29 am by bplus »

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: _KEYDOWN input regardless of case?
« Reply #9 on: March 16, 2020, 11:46:13 am »
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!