08-11-2022, 04:56 PM
A quick little update to handle the keyboard overflow type issue. Note that if you press 7+ keys down, you won't get a report on any key being lifted up until after you release them down to no more than 6 actually being pressed. Keyboards have a natural limit of 6 keys that they can report on at a time, and if you flood them with extra presses, you end up making them not very happy!
Code: (Select All)
Dim Shared As Long KeyBuffer(10), KeyDownKey, KeyUpKey
UpdateKeys = _FreeTimer
On Timer(UpdateKeys, .01) CheckKeys
Timer(UpdateKeys) On
Do
kd = KeyOnlyDown
ku = KeyOnlyup
If kd Then Print "Key"; kd; "Pressed down"
If ku Then Print "Key"; ku; "Released"
_Limit 30
Loop
Sub CheckKeys
K = _KeyHit
Select Case K
Case Is < 0 'keyup
For i = 0 To 10
If KeyBuffer(i) = Abs(K) Then KeyUpKey = Abs(K): KeyBuffer(i) = 0
Next
Case Is > 0 'keydown
For i = 0 To 10
If KeyBuffer(i) = Abs(K) Then Exit For
Next
If i > 10 Then
For i = 0 To 10
If KeyBuffer(i) = 0 Then KeyBuffer(i) = Abs(K): KeyDownKey = Abs(K): Exit Sub
Next
End If
End Select
For i = 0 To 10 'check for lost reports
K = KeyBuffer(i)
If K <> 0 Then
If _KeyDown(K) <> -1 Then
KeyUpKey = K: KeyBuffer(i) = 0 'we missed a key up event - probably due to keyboard reporting limits
Exit Sub
End If
End If
Next
End Sub
Function KeyOnlyDown&
If KeyDownKey <> 0 Then KeyOnlyDown = KeyDownKey: KeyDownKey = 0
End Function
Function KeyOnlyup&
If KeyUpKey <> 0 Then KeyOnlyup = KeyUpKey: KeyUpKey = 0
End Function