Accepting events and SINGLE key presses

I have just discovered that I was totally wrong the above posted example… :frowning:

First of all, usual “key” and “key-up” events are not sent every frame, they are only sent exactly when the key is pressed or released. So, my examples “showing” the difference between usual events and “time-” events are just useless complication. They sill work, but the way they work is absolutely weird. The only good thing about them is that they still show how to use “time-” since there no other such examples on the forum.

Next, if someone still wants to know how to accept single key presses, much more correct way to do that is by using tasks:

from direct.showbase.DirectObject import DirectObject

class Controls(DirectObject):
    def __init__(self):
        self.keyMap = {"jump":0}

        self.accept("space", self.setKey, ["jump", 1])
        # If you want to do something when the key
        # is released, do this:
        #self.accept("space-up", self.setKey, ["jump", -1])

    def setKey(self, key, value):
        self.keyMap[key] = value
        frame = globalClock.getFrameCount()
        taskMgr.add(self.resetKeys, "resetKeys",
                    extraArgs = [key, frame],
                    appendTask = True)

    def resetKeys(self, key, frame, task):
        if globalClock.getFrameCount() > frame:
            self.keyMap[key] = 0
            return Task.done
        else:
            return Task.cont
............................................................
    if controls.keyMap["jump"] == 1:
        JumpHigh() # Check whether jumping is allowed by other conditions and do jump

I hope I didn’t mislead anyone, sorry.