Author Topic: What is the fastest dependable key input method?  (Read 4681 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
What is the fastest dependable key input method?
« on: May 10, 2018, 11:49:36 am »
I've almost exclusively used INKEY$ over the past 30 years. INP is faster, but it is hard to control. Other methods would be PEEK/POKE and _KEYHIT. One obvious problem with INKEY$ is that _LIMIT is required in the loop to prevent your computer from going Fukushima. I would imagine INP has the same problem with high CPU usage if _LIMIT is not used. I don't know if _KEYHIT has the same CPU usage issues. If not, that is a benefit because a mouse routine in a _LIMIT loop is a bit tricky. Limiting the frames can cause mouse input to be missed but I've found a way around that. So mostly it's just about if other calls are being processed while a key routine is being polled, which routine is the fastest (less likely to result in a lagging type response, while being dependable, INKEY$, INP, PEEK/POKE, or _KEYHIT?

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

FellippeHeitor

  • Guest
Re: What is the fastest dependable key input method?
« Reply #1 on: May 10, 2018, 08:53:37 pm »
INKEY$, INP, PEEK and POKE are all emulated, which makes the assumption that one is faster than the other just not true as in the days of yore. That also means they wrap the raw data provided by the OS in a way that mimics DOS days. The data you get by reading _KEYHIT (and _KEYDOWN by analogy) are the direct representation of what the OS is passing to the framework QB64 uses, so I'd recommend going with those.

The requirement for _LIMIT doesn't directly relate to any of these functions but to the fact that you'll want to read input in a loop. You'd better use _LIMIT in a loop even if it's just DO: _LIMIT 30: LOOP, with nothing else in it, for the sake of preserving CPU cycles.
« Last Edit: May 11, 2018, 09:15:44 am by FellippeHeitor »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: What is the fastest dependable key input method?
« Reply #2 on: May 11, 2018, 04:11:19 pm »
But none of what you touched based on has anything to do with why the grass seed I planted in my front yard failed to germinate. This is why I was hoping Steve would reply, too.

Actually, that tells me a lot. Oh, not about my lawn situation, about how QB64 works. That's very interesting about _KEYHIT working directly with the OS, while the other methods are now all emulated.

I think I'll make a small converter function so I can still use all my SELECT CASE statements that use INKEY$ character designation. While I'm not too old to remember those, I think I'm a bit past the age of learning to associate the _KEYHIT scancode numbers.

Thanks for the great explanation. It's amazing you have the time for this... although I suspect you're typing with one hand and changing diapers with the other. Just remember not to powder the keyboard! (Actually I shouldn't joke about baby powder. Talc is a health risk if inhaled. Powders made with cornstarch, instead of talc, are less problematic but still of a concern. Pediatrician organizations do not endorse cornstarch powders, either.)

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

Offline Clippy

  • Newbie
  • Posts: 4
    • View Profile
Re: What is the fastest dependable key input method?
« Reply #3 on: June 06, 2018, 11:12:30 pm »
Biggest problem with key input is with key combinations or pressing 2 arrow keys at once to go diagonally.

INP works better in QB64 with an array and it no longer gets messed up with the Number Lock setting.

Use a Windows function to read keys when not in focus. See you on Tapatalk in 2 years...

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: What is the fastest dependable key input method?
« Reply #4 on: June 07, 2018, 12:36:23 am »
OK, I'll bite... Why in 2 years???

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: What is the fastest dependable key input method?
« Reply #5 on: June 07, 2018, 10:22:52 am »
OK, I'll bite... Why in 2 years???

Pete

I think Ted is making a radically conservative bet on the time it will take to get net54 back up or the least time before he will break down and try Tapatalk again. Just an impression I am getting, of course he might object to my use of adjective (or is it adverb, -ly usually indicates adverbs) "radically".  :D

Well this is how threads get derailed, but since we are this far and "inquiring minds need to know":
https://www.google.com/search?client=opera&q=adverbs+and+adjectives&sourceid=opera&ie=UTF-8&oe=UTF-8
Quote
Some adverbs end in -ly, but not all. ... Instead, the key to telling the difference between adjectives and adverbs is understanding how they work: Adjectives describe a noun or pronoun. Adverbs describe a verb, adjective, or other adverb.
(The bold is missing in quote, I added back in twice where it applies to my question above.)



Update / Edit:
I have picked up the subject of the mystery of decoding a combination key press here:
https://www.qb64.org/forum/index.php?topic=238.15
Reply #23 as it applies to InForm key presses

BTW is this still accurate / relevant to current QB64 version:
Code: QB64: [Select]
  1.   x = _KEYHIT
  2.   IF x THEN
  3.     IF x < 0 THEN  'negative value means key released
  4.       COLOR 2
  5.       PRINT "Released ";
  6.       x = -x
  7.     ELSE
  8.       COLOR 10
  9.       PRINT "Pressed ";   'positive value means key pressed
  10.     END IF
  11.     IF x < 256 THEN    'ASCII code values
  12.       PRINT "ASCII "; x;
  13.       IF x >= 32 AND x <= 255 THEN PRINT "[" + CHR$(x) + "]" ELSE PRINT
  14.     END IF
  15.     IF x >= 256 AND x < 65536 THEN '2 byte key codes
  16.       PRINT "2-BYTE-COMBO "; x AND 255; x \ 256;
  17.       x2 = x \ 256
  18.       IF x2 >= 32 AND x2 <= 255 THEN PRINT "[" + CHR$(x2) + "]" ELSE PRINT
  19.     END IF
  20.     IF x >= 100000 AND x < 200000 THEN      'QB84 Virtual Key codes
  21.       PRINT "SDL VK"; x - 100000
  22.       END IF
  23.       IF x >= 200000 AND x < &H40000000 THEN
  24.             PRINT "QB64 VK"; x - 200000
  25.     END IF
  26.     IF x >= &H40000000 THEN              'Unicode values (IME Input mode)
  27.       PRINT "UNICODE "; x - &H40000000; "0x" + HEX$(x - &H40000000) + " ...";
  28.       cx = POS(1): cy = CSRLIN
  29.       _FONT unifont
  30.       LOCATE cy, cx
  31.       COLOR 15
  32.       z$ = MKL$(x - &H40000000) + MKL$(0)
  33.       PRINT z$ + z$ + z$;
  34.       _FONT font
  35.       LOCATE cy, 1: PRINT
  36.     END IF
  37.   END IF
  38.  
It is from:
http://qb64.org/wiki/KEYHIT
that I used to try and make sense of __UI_KeyHit values.  I ask because WTH? is SDL VK?
« Last Edit: June 07, 2018, 01:34:50 pm by bplus »