Author Topic: InKey Codes vs KeyHit codes  (Read 2470 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
InKey Codes vs KeyHit codes
« on: December 31, 2021, 02:18:51 am »
As I was discussing over at the Discord channels, here's a little tidbit about our key codes which a lot of people seem unaware of -- INKEY$ and _KEYHIT are the EXACT same codes!  The only difference is INKEY converts the codes to string format, while _KEYHIT keeps them as integer values.

To illustrate this, have fun pressing and holding keys down on your keyboard with the following code:

Code: QB64: [Select]
  1.     Cls
  2.     i$ = InKey$
  3.     k = _KeyHit
  4.     If Len(i$) > 1 Then '2 digit code
  5.         Print "INKEY$ = 0 + "; Asc(i$, 2); " or "; CVI(i$)
  6.     ElseIf i$ <> "" Then
  7.         Print "INKEY$ = "; Asc(i$); " or "; CVI(i$ + "0")
  8.     Else
  9.         Print "INKEY$ = NULL"; " or ERROR"
  10.     End If
  11.     Print "_KEYHIT = "; k; " or "; k Mod 256; k \ 256
  12.     _Limit 30
  13.     _Display
  14.  

If we use CVI (the QB64 command to convert a 2-byte string to an integer value) on INKEY$, we can see what values it gives us for any key pressed.

We can then compare those values to what KEYHIT gives us...

And, you guys might not believe this....

They're the *EXACT* same values.

There's no difference between INKEY$ and _KEYHIT except for the rather obvious one -- one returns the value as a 2-byte string while the other returns the value as a 2-byte integer. 

At their root, the only real difference between them is one is basically STR$(keycode) while the other is simply the VAL(keycode). 

NOTE:  (Actually it's a MKI$(keycode), but a lot of people are unfamiliar with converting numbers to strings with the MK$ commands, so I used STR$ in the previous sentence as *EVEYONE* tends to learn STR and VAL as some of their first commands with the language to help them highlight the difference between the INKEY$ values and KEYHIT values.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: InKey Codes vs KeyHit codes
« Reply #1 on: December 31, 2021, 04:08:39 am »
I added a 'Sleep' command to see what was happening on screen before informations dispeared under "NULL" or "ERROR" message again !

Interesting to compare both commands. I use ' inkey$ ' (old basic habit...)




Why not yes ?

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: InKey Codes vs KeyHit codes
« Reply #2 on: December 31, 2021, 07:22:29 am »
That confirms what I assumed ever since I watched your vid on big-endian/little-endian byte storage, but never bothered to investigate in quite this way. The two byte code values are flipped integer values when using CHR$, then used as straight up numbers for _KEYHIT & _KEYDOWN.

I should play around with _KEYHIT some, up until this point I've been mostly an INKEY$ & _KEYDOWN coder.