QB64 Phoenix Edition
Thinking about updating _KEYHIT in the wiki... - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Works in Progress (https://qb64phoenix.com/forum/forumdisplay.php?fid=9)
+---- Thread: Thinking about updating _KEYHIT in the wiki... (/showthread.php?tid=2959)



Thinking about updating _KEYHIT in the wiki... - Pete - 08-17-2024

Code: (Select All)
Dim x, oldx(3) As Long
Dim i, cnt, cy, cx As Integer
Screen _NewImage(800, 600, 8)
Cls
font = _LoadFont("cyberbit.ttf", 24)
unifont = _LoadFont("cyberbit.ttf", 24, "UNICODE")
_Font font
Do
    x = _KeyHit
    If x And x <> oldx(1) And x <> oldx(2) And x <> oldx(3) Then
        Select Case x
            Case Is > 0
                Color 10
                Print "Pressed "; ' Positive value means key was pressed.
                cnt = cnt + 1 ' Tracking on.
                oldx(cnt) = x
            Case Else ' Negative value means key was released.
                Color 2
                Print "Released ";
                x = -x
                i = 0: Do ' Tracking off.
                    i = i + 1
                    If oldx(i) = x Then cnt = cnt - 1: oldx(i) = 0: Exit Do
                Loop Until i = 3
        End Select
        Select Case x
            Case Is < 256
                If x < 256 Then ' ASCII code values.
                    Print "ASCII "; x;
                    If x >= 32 And x <= 255 Then Print "[" + Chr$(x) + "]" Else Print
                End If
            Case 256 To 65536 ' 2 byte key codes.
                Print "2-BYTE-COMBO "; x And 255; x \ 256;
                x2 = x \ 256
                If x2 >= 32 And x2 <= 255 Then Print "[" + Chr$(x2) + "]" Else Print
            Case 100000 To 199999 ' QB64 Virtual Key codes.
                Print "SDL VK"; x - 100000
            Case 200000 To &H3FFFFFFF
                Print "QB64 VK"; x - 200000
            Case Is >= &H40000000 ' Unicode values. (IME Input mode)
                Print "UNICODE "; x - &H40000000; "0x" + Hex$(x - &H40000000) + " ...";
                cx = Pos(1): cy = CsrLin
                _Font unifont
                Locate cy, cx
                Color 15
                z$ = MKL$(x - &H40000000) + MKL$(0)
                Print z$ + z$ + z$;
                _Font font
                Locate cy, 1: Print
        End Select
    End If
Loop

This modification of the existing version adds tracking the keys so it only prints to the screen once, allowing us to hold down key combinations without the screen scrolling all over hell and half of Georgia. (I've always wondered what half of Georgia that is?)

What I don't get is how to test the unicode case. I'm using a standard laptop keyboard. Any ideas???

Interesting that there are some keys that won't allow a 3 key press like A+S+W. The W will not register. That's probably because those keys are sometimes involved in arrow directions.

Since this would be for the wiki, I'm open to any suggestions, modifications, etc. but I get it, wiki examples should be as short and to the point as possible. Prettying things up is the readers responsibility.

Pete