keyup, keydown,slowkeydown
#6
(08-11-2022, 03:42 PM)bplus Wrote: Steve is always 2 steps ahead of us, he's probably talking about future developments :-))

_KEYUP(kh&) = NOT _KEYDOWN(kh&)

Actually, I was thinking we had some simple commands somewhere which only listed when keys went up and down.  Apparently we don't and those were library functions which I'd written once.  My apologies.

For an example of what I'm talking about, try this little code:

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
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

This *only* lists when a key goes down, or when a key comes up.  No repeat messages from it!  Just glancing over James's code, I saw the comment "only returns positive values when a key is pressed", so my brain was thinking it was working in a similar manner.

What we have above is a set of routines which tells you *only* when a key is pressed down, or when a key is released.  Of course, most keyboards only report 6 keys at a time or so, due to basic USB limitations, so if you want to go crazy and press a dozen keys down at once and hold them all, you'll probably end up glitching out the buffer here as I didn't write this little demo to deal with something that complex.
Reply


Messages In This Thread
keyup, keydown,slowkeydown - by James D Jarvis - 08-10-2022, 09:04 PM
RE: keyup, keydown,slowkeydown - by SMcNeill - 08-11-2022, 10:40 AM
RE: keyup, keydown,slowkeydown - by BSpinoza - 08-11-2022, 01:06 PM
RE: keyup, keydown,slowkeydown - by bplus - 08-11-2022, 03:42 PM
RE: keyup, keydown,slowkeydown - by SMcNeill - 08-11-2022, 04:53 PM
RE: keyup, keydown,slowkeydown - by SMcNeill - 08-11-2022, 04:56 PM
RE: keyup, keydown,slowkeydown - by PhilOfPerth - 08-12-2022, 11:01 AM



Users browsing this thread: 2 Guest(s)