Suggestion Implementing a KeyReleased Method...

SimonVD

New member
Tutorial Author
Joined
Dec 22, 2019
Messages
39
I've changed the post title from "KeyUp" to "KeyReleased."

--------------------------------------------------------------------

I've implemented the code below for my own use...
Perhaps someone will find it useful.

Code:
Strict

Import Mojo

Class KeysDownMap<T> Extends Map<Int, T>
    Method Compare:Int(key1:Int, key2:Int)
        Return key1 - key2
    End Method
End Class

Class Game Extends App

    Private
    Field keysDown:KeysDownMap<Int> = New KeysDownMap<Int>()

    Public
    Method AppKeyUp:Int(keyNo:Int)
        Local returnValue:Int = 0
        If keysDown.Contains(keyNo)
            If keysDown.Get(keyNo) = 1 And KeyDown(keyNo) = 0
                returnValue = 1 ' Meaning this key is up
                keysDown.Remove(keyNo)
            Endif
        Endif
        AppKeyDown(keyNo)
        Return returnValue
    End Method

    Method AppKeyDown:Int(keyNo:Int)
        Local isKeyDown:Int = KeyDown(keyNo)
        If isKeyDown And keysDown.Contains(keyNo) = False
            keysDown.Set(keyNo, 1)
        Endif
        Return isKeyDown
    End Method

    Method OnUpdate:Int()
       
        If AppKeyUp(KEY_CONTROL) Then Print "Control key is up"
        If AppKeyUp(KEY_A) Then Print "A key is up"
        If AppKeyUp(KEY_SHIFT) Then Print "Shift key is up"
   
        Return 0
    End Method

End Class

Function Main:Int()
    New Game
    Return 0
End
 
Last edited:

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,167
Why not simply use this?
Cerberus:
If KeyDown(KEY_CONTROL)= False Then Print "Control key is up"
 

SimonVD

New member
Tutorial Author
Joined
Dec 22, 2019
Messages
39
Why not simply use this?
Cerberus:
If KeyDown(KEY_CONTROL)= False Then Print "Control key is up"

Probably I didn't get it, I'm sleepy but... when I copy pasted the code you suggested... it prints "Control key is up" all the time.

I need to know the up event coming after the down event for a particular key. Say, a key is down now but when will it come up? So that I could do things when the key is up. In between down and up events there could be many frames... and also I need to track multiple keys...

Something like this:

If you have some time please copy the KeyUp KeyReleased code I shared, paste it, run it, and try to press CTRL, A and SHIFT keys, you'll see.

Is there already a method to do this? I couldn't find it.
 
Last edited:

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
643
You are talking about a KeyReleased() Function!? I can understand the wish for this. KeyUp() would just be the wrong name as its kind of the opposite to KeyDown().
 
Top Bottom