Author Topic: detecting keypresses question - _KEYDOWN and _BUTTON both miss certain keys  (Read 5880 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: detecting keypresses question - _KEYDOWN and _BUTTON both miss certain keys
« Reply #15 on: December 23, 2020, 11:40:15 am »
In a word... YES.

That drove me bat sat crazy, too. I've used it in a C keyboard input program I wrote, many months ago. It works, but I hate the way it polls the memory. You would think there would be a more efficient method. Sometimes I really miss the days of fixed memory PEEK.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: detecting keypresses question - _KEYDOWN and _BUTTON both miss certain keys
« Reply #16 on: December 23, 2020, 03:09:13 pm »
In a word... YES.

That drove me bat sat crazy, too. I've used it in a C keyboard input program I wrote, many months ago. It works, but I hate the way it polls the memory. You would think there would be a more efficient method. Sometimes I really miss the days of fixed memory PEEK.

Pete

You can read a buffer of the 256 keys which it checks instead, if you like, and do it all at once.

Basically, instead of READ key, you'd READ keyboard(), where keyboard is dimmed as DIM keyboard(1 TO 256) AS_UNSIGNED_BYTE.

Of course, after you read that whole array, you'd still have to poll it to see which key(s) are in use, so I really don't see where you're any faster/more efficient...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: detecting keypresses question - _KEYDOWN and _BUTTON both miss certain keys
« Reply #17 on: December 23, 2020, 03:36:10 pm »
My hunch is that method would be slower. My other hunch is that my first hunch is your hunch too. Lucky for me, my past profession allows me to effectively deal with most hunches.

Anyway, did you see this post about Ctrl + Tab? https://www.qb64.org/forum/index.php?topic=3402.msg127094#msg127094

That's about as close as I could get using "old school" QB coding. Apparently PEEK(197) is no longer supported, so using it in conjunction with PEEK(1047), which is still supported, would no longer be an option.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: detecting keypresses question - _KEYDOWN and _BUTTON both miss certain keys
« Reply #18 on: December 24, 2020, 11:22:31 am »
Does the library being discussed execute this entire loop every time it tries to detect a single keypress? Scans all 10000?

For _BUTTON, you're only looping to 512:
Code: QB64: [Select]
  1.         FOR iLoop = 1 TO 512
  2.             iCode = _BUTTON(iLoop)
  3.  

But if you know what keys you're looking for, no looping is necessary, you just read the specific key codes, eg.
Code: QB64: [Select]
  1.         ' Key                     _BUTTON       Action
  2.         ' ---------------------   -----------   --------
  3.         ' A                       31            left player 1
  4.         ' S                       32            right player 1
  5.         ' W                       18            up player 1
  6.         ' Z                       45            down player 1
  7.         ' Left Ctrl               30            fire player 1
  8.  
  9.         ' Left                    332           left player 2
  10.         ' Right                   334           right player 2
  11.         ' Up                      329           up player 2
  12.         ' Down                    337           down player 2
  13.         ' Right Ctrl              286           fire player 2
  14.  
  15.         ' KEYPAD 4 Left           76            left player 3
  16.         ' KEYPAD 6 Right          78            right player 3
  17.         ' KEYPAD 8 Up             73            up player 3
  18.         ' KEYPAD 2 Down           81            down player 3
  19.         ' KEYPAD 0 Ins            83            fire player 3
  20.        
  21.         IF _BUTTON(31) THEN ' KEY "A" IS CURRENTLY DOWN
  22.                 arrKeyState(31) = TRUE ' if we want to track state
  23.                 '(move player 1 left)
  24.         END IF
  25.         IF _BUTTON(32) THEN ' KEY "S" IS CURRENTLY DOWN
  26.                 arrKeyState(32) = TRUE ' if we want to track state
  27.                 '(move player 1 right)
  28.         END IF
  29.         etc.
  30.  

That's just an example, you could use a SELECT CASE or any number of methods.