• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

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:
Why not simply use this?
Cerberus:
If KeyDown(KEY_CONTROL)= False Then Print "Control key is up"
 
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:
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().
 
Back
Top Bottom