Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyhit to Inkey Conversion Project
#1
Code: (Select All)
Do
_Limit 30
k = _KeyHit
c$ = InKey$
If k > 0 Then Cls
InkeyVert b$, k, kp, alt, ctrl
If alt Then Locate 1, 1: Print "Alt Down...";
If ctrl Then Locate 1, 1: Print "Ctrl Down...";
If kp Then Locate 2, 1: Print "Keyhit Value: "; kp; Space$(15);
If Len(b$) = 1 Then Locate 3, 1: Print "KeyHit Conversion: "; b$; " ("; LTrim$(Str$(Asc(b$))); ")"; Space$(15);
If Len(b$) = 2 Then Locate 3, 1: Print "KeyHit Conversion: "; "Chr$(0) + "; b$; " ("; LTrim$(Str$(Asc(Right$(b$, 1)))); ")"; Space$(15);
If Len(c$) = 1 Then Locate 4, 1: Print "Inkey$ Comparison: "; c$; " ("; LTrim$(Str$(Asc(c$))); ")"; Space$(15);
If Len(c$) = 2 Then Locate 4, 1: Print "Inkey$ Comparison: "; "Chr$(0) + "; c$; " ("; LTrim$(Str$(Asc(Right$(c$, 1)))); ")"; Space$(15);
If Len(c$) And kp And b$ <> c$ Then Locate 15, 1: Print "Mismatch! Enter to resume... KeyHitConvert$ = "; b$; " Inkey$ = "; c$; " ";: Sound 100, 1: Do: _Limit 10: Loop Until InKey$ = Chr$(13): _KeyClear: _Delay .5: Cls
Loop

Sub InkeyVert (b$, k, kp, alt, ctrl)
Static initiate, AltNumMap$, AltShiftNumMap$
If initiate = 0 Then
initiate = _TRUE
_ControlChr Off
AltNumMap$ = "œ®¬žŸ ¡•¢£¤°¯–—Ž‘’”­«“ª œ®¬žŸ ¡•¢£¤°¯–—Ž‘’”­«“ª"
AltShiftNumMap$ = "!@#$%^&*()"
End If
b$ = ""
Select Case k
Case -100306 To -100305, 100305 To 100306 ' Ctrl
If k > 0 Then ctrl = 1 Else ctrl = 0: Locate 1, 1: Print Space$(15);
Case -100308 To -100307, 100307 To 100308 ' Alt
If k > 0 Then alt = 1 Else alt = 0: Locate 1, 1: Print Space$(15);
Case -255 To -32, 32 To 255
If k > 0 Then kp = k Else kp = 0
If kp Then b$ = Chr$(kp)
Case Else
If k > 0 Then kp = k Else kp = 0
If kp Then
If kp \ 256 < 255 Then
If kp < 10000 Then
b$ = Chr$(kp) ' Example: Tab key.
Else
b$ = Chr$(0) + Chr$(kp \ 256)
End If
Else
Locate 3, 1: Print Space$(_Width * 2); ' CapsLock
End If
End If
End Select
If alt And kp <> 0 And kp < 100000 Then
Select Case kp
Case 65 To 90, 97 To 122 ' A - Z and a - z
x$ = Chr$(Asc(Mid$(AltNumMap$, kp - 64, 1)) - 126)
b$ = Chr$(0) + x$
Case 15104 To 17408 ' F1 - F10
b$ = Chr$(0) + Chr$(kp \ 256 + 45)
Case 34048, 34304 ' F11 and F12
b$ = Chr$(0) + Chr$(kp \ 256 + 6)
Case 48
b$ = Chr$(0) + Chr$(129) ' 0
Case 49 To 57 ' 1 - 9
b$ = Chr$(0) + Chr$(kp + 71)
Case 33, 64, 35, 36, 37, 94, 38, 42, 40, 41
x$ = AltShiftNumMap$
b$ = Chr$(0) + Chr$(119 + InStr(x$, Chr$(kp)))
Case 18176 To 21248 ' Insert, Delete, Home, End, Page and Arrow Keys.
b$ = Chr$(0) + Chr$(kp \ 256 + 80)
Case 8: b$ = Chr$(0) + Chr$(14) ' Backspace
Case 32: b$ = "" ' (Space) with and without Shift has no conversion.
Case 126, 96: b$ = Chr$(0) + Chr$(41) ' ~ `
Case 95, 45: b$ = Chr$(0) + Chr$(130) ' _ -
Case 43, 61: b$ = Chr$(0) + Chr$(131) ' + =
Case 123, 91: b$ = Chr$(0) + Chr$(26) ' { [
Case 125, 93: b$ = Chr$(0) + Chr$(27) ' } ]
Case 124, 92: b$ = Chr$(0) + Chr$(43) ' | \
Case 58, 59: b$ = Chr$(0) + Chr$(39) ' : ;
Case 34, 39: b$ = Chr$(0) + Chr$(40) ' " '
Case 60, 44: b$ = Chr$(0) + Chr$(51) ' < ,
Case 62, 46: b$ = Chr$(0) + Chr$(52) ' > .
Case 63, 47: b$ = Chr$(0) + Chr$(53) ' ? /
Case Else
Locate 7, 1: Print "Pete missed something! Enter to resume... k =", k: Sound 100, 1: Do: _Limit 10: Loop Until InKey$ = Chr$(13): _KeyClear: _Delay .5: Cls
End Select
End If
If ctrl And kp <> 0 And kp < 100000 Then
Locate 1, 1: Print "Ctrl Down...";
Select Case kp
Case 0 To 12, 14 To 32
b$ = Chr$(kp) ' Example: Ctrl + "{" or "[".
Case 13 ' Examples: Ctrl + J or Ctrl + Enter.
b$ = Chr$(10)
Case 65 To 90, 97 To 122 ' Example: Most Ctrl combos.
b$ = Chr$(kp Mod 32)
Case 15000 To 17408
b$ = Chr$(0) + Chr$(kp \ 256 + 35) ' F1 - F10
Case 34048 To 34304 ' F11 - F12
b$ = Chr$(0) + Chr$(kp \ 256 + 4)
Case 18176 ' Home
b$ = Chr$(0) + Chr$(119)
Case 18688 ' PgUp
b$ = Chr$(0) + Chr$(132)
Case 20224 ' End
b$ = Chr$(0) + Chr$(117)
Case 20736 ' PgDn
b$ = Chr$(0) + Chr$(118)
Case 20992 ' Insert
b$ = Chr$(0) + Chr$(146)
Case 18432 ' Arrow Up
b$ = Chr$(0) + Chr$(141)
Case 19200 ' Arrow Lt
b$ = Chr$(0) + Chr$(115)
Case 19712 ' Arrow Rt
b$ = Chr$(0) + Chr$(116)
Case 20480 ' Arrow Dn
b$ = Chr$(0) + Chr$(145)
Case 21248 ' Example: Ctrl + Backspace or Ctrl + Delete - Both are 21248.
b$ = Chr$(0) + Chr$(kp \ 256 + 64)
Case Else
Locate 7, 1: Print "Pete missed something! Enter to resume... k =", k: Sound 100, 1: Do: _Limit 10: Loop Until InKey$ = Chr$(13): _KeyClear: _Delay .5: Cls
End Select
End If
End Sub

I wish there were some more math tricks to cut down on all the cases. Kudos Steve for the MOD 32 method, which cut out the need to check upper and lower case for the Ctrl key. I need to go back and give him a +2 for that!

Anyway, it should be able to translate any _Keyhit value into an Inkey$ value. So now old relics like me who can't get out of arrow up is Chr$(0) + "H" can use a keyhit routine but still get the same  Chr$(0) + "H" output! It's sizable, because it's more involved than just straight letters and numbers. It also translates combos like Ctrl + V, Alt + F, etc. All keys and key combinations, at least that's the goal. I put a couple of case else error traps in this first release just in CASE I missed something(s).

Pete
Reply
#2
Careful with just automatically substituting CHR$(10) for chr$(13) with CTRL.  It’s true for CTRL-J, but 13 is *also* the code for ENTER.  

You just remapped Ctrl-Enter to Ctrl-J.

Honestly, I don’t know how you’d easily distinguish between the two with GLUT screwing up crap with you.
Reply
#3
(12-31-2025, 06:43 AM)SMcNeill Wrote: Careful with just automatically substituting CHR$(10) for chr$(13) with CTRL.  It’s true for CTRL-J, but 13 is *also* the code for ENTER.  

You just remapped Ctrl-Enter to Ctrl-J.

Honestly, I don’t know how you’d easily distinguish between the two with GLUT screwing up crap with you.
@SMcNeill

It should be fine, as coded. 

INKEY$: Ctrl + Enter returns Chr$(10), not 13.

Extracted from post 1 and made so just ctrl + J, Ctrl + Enter, and Enter by itself are output.
Code: (Select All)
Do
    _Limit 30
    k = _KeyHit
    Select Case k
        Case -100306 To -100305, 100305 To 100306 ' Ctrl
            If k > 0 Then ctrl = 1 Else ctrl = 0
    End Select
    If ctrl And k = 13 Then k$ = Chr$(10) ' Convert here, but use k$ for visual.
    b$ = InKey$
    If b$ = Chr$(10) Then Print "b$ = Chr$(10)"
    If b$ = Chr$(13) Then Print "b$ = Chr$(13)"
    If Len(k$) Then Print "_Keyhit Conversion = Chr$(10)": k$ = ""
Loop

Pete
Reply
#4
I updated the initial post. I may have to try the Windows virtual keyboard for more cases to address, but if anyone finds something I missed, please let me know.

Pete
Reply
#5
@Pete help me understand this a little more. I understand you made this to help you convert to a new habit of coding...

On mac when I hit ctrl+alt+shift+s I get :

[Image: SCR-20260109-hb1.png]
https://shottr.cc/s/2xla/SCR-20260109-hb1.png
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#6
(01-09-2026, 04:47 PM)grymmjack Wrote: @Pete help me understand this a little more. I understand you made this to help you convert to a new habit of coding...

On mac when I hit ctrl+alt+shift+s I get :

[Image: SCR-20260109-hb1.png]
https://shottr.cc/s/2xla/SCR-20260109-hb1.png
That doesn't occur in Windows. It's a Mac thing. In Windows, you can only do 3 keys, max.

Nice to know, but I don't have a Mac to do the extra mapping needed.

Pete
Reply
#7
(01-10-2026, 03:31 AM)Pete Wrote:
(01-09-2026, 04:47 PM)grymmjack Wrote: @Pete help me understand this a little more. I understand you made this to help you convert to a new habit of coding...

On mac when I hit ctrl+alt+shift+s I get :

[Image: SCR-20260109-hb1.png]
https://shottr.cc/s/2xla/SCR-20260109-hb1.png
That doesn't occur in Windows. It's a Mac thing. In Windows, you can only do 3 keys, max.

Nice to know, but I don't have a Mac to do the extra mapping needed.

Pete

That's not a QB64 limitation; that's a "Pete is a cheap ass and owns a really shitty keyboard" limitation.   Go out and buy yourself a nice frigging PC right now!!

Code: (Select All)
Open "temp.txt" For Output As #1
Do
    Print , "Keystate"
    Print #1, "Keystate"
    For i = 1 To 1000000
        If _KeyDown(i) Then Print #1, i: Print i
    Next
    _Display
    _Delay 1
Loop


Quote:Keystate
13
65
68
70
71
72
83
100304
100306
100308

From the simple code above, I'm getting 10 keypresses and ... ran out of fingers to hold down keys.  Mine is a keyboard with unlimited key rollover, whereas apparently you have an el cheapo keyboard with a 3-key limit for pressing keys.

For the love of god man, pony up a penny or two and upgrade from your stone-age caveman PC!  What the heck are you using??  An abacus?!

https://www.howtogeek.com/789557/what-is...over-nkro/

Reply
#8
Fine, fine, fine... I'll go order a new PC when my hangnail fully heals. Right now it just hurts too damn much to make it past the fourth digit of the PC company sales department number on my party line rotary phone.

Pete Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  new approach to base conversion system and math library Dragoncat 2 574 07-16-2025, 10:19 PM
Last Post: Dragoncat
  universal base conversion program possibly for a base conversion library later on Dragoncat 22 3,021 04-24-2025, 02:08 AM
Last Post: Dragoncat
  Endian conversion...Part 2, The Endian Is Nigh! tantalus 0 395 03-22-2025, 09:43 PM
Last Post: tantalus
  Text Menu Library Project Pete 3 728 01-03-2025, 05:55 PM
Last Post: Pete
  Keyhit library update SMcNeill 12 2,641 02-10-2023, 08:31 PM
Last Post: johannhowitzer

Forum Jump:


Users browsing this thread: