Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand keyhit function
#2
(08-09-2022, 02:52 AM)PhilOfPerth Wrote: Trying to get my tiny brain around the _keyhit command... It seems to switch to the negative key value when the key is released, but with this simple piece, it seems to have retained its positive value. Where am I going wrong?
Code: (Select All)
Color 0, 15: Cls
Message:
k = 0 '                       I need this because _keyhit retains its positive (key pressed) value
Print "Press A or B"

Do Until k > 0 '              do this until _keyhit is positive
    k = _KeyHit '             set k to _keyhit value
Loop
'                             now leave loop with k set at positive value from _keyhit
Select Case k
    Case Is = 65, 97 '        "A" pressed
        ResponseA
    Case Is = 66, 98 '        "B" pressed
        ResponseB
    Case Else
        WrongKey
End Select
Cls
GoTo Message

Sub ResponseA
    Print "A pressed"
    Sleep 1
End Sub

Sub ResponseB
    Print "B pressed"
    Sleep 1
End Sub

Sub WrongKey
    Print "Wrong Key!"
    Sleep 1
End Sub

You're checking the value of "k" instead of the function. In "Select Case" clause. It's because you have it on a loop and on second execution, without "k = 0" before it gets to "Do Until k > 0" which causes it to never execute the body of that loop.
Reply


Messages In This Thread
RE: Trying to understand keyhit function - by mnrvovrfc - 08-09-2022, 04:03 AM



Users browsing this thread: 2 Guest(s)