Author Topic: Trouble with _KEYDOWN for a game?  (Read 2567 times)

0 Members and 1 Guest are viewing this topic.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Trouble with _KEYDOWN for a game?
« on: May 26, 2020, 12:42:02 am »
Hello all, I'm new so I'm sorry to bug you guys with a question in my first post, though I'm sure that happens a lot. I'm also sorry if this has been brought up before but I wasn't able to turn anything up on the Google.

I'm trying to use the _KEYDOWN function for input to a game. Specifically I'm trying to use the A and D keys to move left and right, and the right shift key to jump. However, for moving right, for example, I'm using "_KEYDOWN(68) OR _KEYDOWN(100)" so that the player still will move even if the user leaves CAPS LOCK turned on or if the player is trying to move and jump at the same time. Like so:

Code: QB64: [Select]
  1.     'Walk when movement keys are pressed.
  2.     IF _KEYDOWN(68) OR _KEYDOWN(100) THEN
  3.         plr.mirror = 0
  4.         IF plr.x + PLR_WALK_SPEED <= GAME_WIDTH - plr.w THEN
  5.             plr.x = plr.x + PLR_WALK_SPEED
  6.         ELSE
  7.             plr.x = GAME_WIDTH - plr.w
  8.         END IF
  9.         plr.mirror = -1
  10.         IF plr.x - PLR_WALK_SPEED >= 0 THEN
  11.             plr.x = plr.x - PLR_WALK_SPEED
  12.         ELSE
  13.             plr.x = 0
  14.         END IF
  15.     ELSE
  16.         plr.walkanim.currentframe = 0
  17.         plr.walkanim.frametime = 0
  18.     END IF

The problem is that the A or D key will "stick", that is if I'm jumping and moving right, and release the D key before releasing Shift, the game thinks the D key is still pressed and the player continues moving to the right until the D key is pressed and released again.

My first thought was this had to be a bug with the _KEYDOWN function but being that I'm new to QB64 it's more likely an error on my part, that I'm somehow not using the function properly. Can anyone help me shed some light on this? Thank you in advance for your help!

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #1 on: May 26, 2020, 01:50:56 am »
After some additional searching of the forum and additional testing, I've decided this must be a hardware issue with my keyboard. If I use the left/right arrow keys with left shift, the problem goes away. Stupid laptop keyboard. I'm thinking about grabbing my USB keyboard from the other computer and trying that just to see if I can replicate the issue there.

Sigh.

Please let me know if there's something I'm missing but I'm thinking this is a mystery solved.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #2 on: May 26, 2020, 07:15:56 am »
Welcome to the forum Nate5700

Glad you were able to get things percolating. Check out the _KEYDOWN page on the wiki, it will have a list of keyboard codes generated by _KEYDOWN
http://www.qb64.org/wiki/KEYDOWN

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #3 on: May 26, 2020, 10:33:11 am »
Welcome to the forum! I have some example uses of _KEYDOWN located here:

http://qb64sourcecode.com/Task9.html#KEYDOWN
In order to understand recursion, one must first understand recursion.

FellippeHeitor

  • Guest
Re: Trouble with _KEYDOWN for a game?
« Reply #4 on: May 26, 2020, 10:34:36 am »
Quote
I've decided this must be a hardware issue with my keyboard

It very likely is a hardware limitation indeed. All _KEYDOWN does is read whatever the OS is returning regarding those keys.

Welcome to the forum!

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #5 on: May 26, 2020, 01:45:00 pm »
Thanks all for the welcomes, and for the helpful links. I'm enjoying writing programs with QB64 so far, so hopefully I can be a helpful participant on the forum. QBASIC was my first exposure to programming back when I was around 10 years old, which was...*looks at watch* 26 years ago now. Wow I feel old. But because of that I was extremely excited to find out QB64 was a thing.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #6 on: May 26, 2020, 01:54:46 pm »
Hi Nate,

Welcome, yeah old, I discovered GW Basic in '89 and QB45 shortly after.

We've talked about this a number of times, the difference between using _KEYDOWN and _KEYHIT.

Depends how you want to use them in a loop, and what you want to trap but I'd say use _KEYDOWN for detecting key combinations shift + something, or alternate or ctrl and use _KEYHIT for arrows or letters, more specific things.

_KEYCLEAR comes in handy for clearing _KEYHIT but is useless for _KEYDOWN which never "clears" because it only reports if key is pressed or not when polled.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #7 on: May 26, 2020, 04:56:52 pm »
Depends how you want to use them in a loop, and what you want to trap but I'd say use _KEYDOWN for detecting key combinations shift + something, or alternate or ctrl and use _KEYHIT for arrows or letters, more specific things.

I'm finding that for gaming _KEYDOWN is much more effective. When I thought _KEYDOWN was at issue with my problem I tried switching to _KEYHIT. I may not have been using that particular function properly either, but I found it to be very jerky and inconsistent on whether it detected the movement keys being pressed. For a game where the user is going to be mashing several buttons at a time, _KEYDOWN seems to be the way to go, even if you're not looking for particular keys like SHIFT, ALT, etc. At least for what I've seen so far.

It's my first time trying to do it in QB64 but this isn't my first go around at attempting to write a game. It seems like all the gaming libraries I've looked at have all had a function similar to _KEYDOWN that just detects whether the key is pressed or not, specifically for the purpose of input to the game.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #8 on: May 26, 2020, 05:34:34 pm »
Take a look at Task 9 in my tutorial: http://qb64sourcecode.com/Task9.html

I go over each method of keyboard input along with example programs to accompany them.

_KEYHIT has many of the same limitations as the INKEY$ command mainly being the buffered input and slow repeat rate. Not really great for games that need instant keyboard response. In order to implement _KEYHIT for fast action games you'll need to create a latching variable and then detect when the key has been released which _KEYHIT can do as well.
« Last Edit: May 26, 2020, 05:38:24 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: Trouble with _KEYDOWN for a game?
« Reply #9 on: May 26, 2020, 06:07:44 pm »
Take a look at Task 9 in my tutorial: http://qb64sourcecode.com/Task9.html

I go over each method of keyboard input along with example programs to accompany them.

_KEYHIT has many of the same limitations as the INKEY$ command mainly being the buffered input and slow repeat rate. Not really great for games that need instant keyboard response. In order to implement _KEYHIT for fast action games you'll need to create a latching variable and then detect when the key has been released which _KEYHIT can do as well.

I'll have to give this a closer look! Funny, I actually had found your tutorial before I joined this forum and it was already open in a tab in my browser, I just hadn't read through it fully yet!

_KEYDOWN seems to definitely be working for my purposes. I changed the code so that you can use A and D for movement with RCTRL for jump and it works fine. Something about those keys with Shift seems to be funky on this keyboard, for sure.