Camera Control and Polling

I am implementing a camera class that basically will allow the user to move the camera with smooth motion. I.E.: the speed of camera increases to a maximum as long as the a key is pressed in. If the keyboard key isn’t pressed anymore, then the speed of camera decreases steadily and smoothly to 0.

this is my camera keyboard loading method:

    def load_keyboard(self):
        '''loads up the event handlers for camera motion keyboard control'''
        self.accept('arrow_up', self.move_up)
        self.accept('arrow_down', self.move_down)
        self.accept('arrow_left', self.move_left)
        self.accept('arrow_right', self.move_right)
        self.accept('a', self.radius_down)
        self.accept('z', self.radius_up)     
        # Sets up the monitor for polling the key states
        keys = ['arrow_up', 'arrow_down', 'arrow_left', 'arrow_right',
                'a', 'z']
        for key in keys:
  , key, key+'-up', False)

I use a combination of event handlers and polling.
Where the self.move_left method for example will do this:

    def move_left(self):
        self.h_speed -= self.increment * globalClock.getDt()
        if self.h_speed < -self.max_speed:
            self.h_speed = -self.max_speed

Now the thing is, when you press in the the keys, it seems like it’s initially accepting the key repeat events much slower than about 1 sec later where it all of a sudden seems to repeat the key events much faster. Is this a operating system thing or can I control it?

Is this expected or am I hallucinating? Should I turn completely to use polling instead? Let me know if I explained myself clearly.



The frequency of key-repeat is an operating system parameter. I would not recommend relying on key-repeat; rather, you should track the occurrence of key-down and key-up messages to determine whether a key is currently being held down.

When a key is pressed, you will receive the key-down event. You will receive the corresponding key-up message when the key is released later. In between, you may receieve additional key-down messages due to key repeat; you should ignore these.

So between the first key-down and the next key-up message, the key is being held down, and you can implement a task that times how long the key has been held down and does the appropriate thing.


Beautiful!! My smooth camera :slight_smile:

Yea, initially I was only using polling to see if the keys are not pressed anymore, therefore to reduce the speeds.

But now, I’m using polling for speeding up the cam as well and not use event handler anymore, everything works good!!!

I tried to not use polling cuz the doc said “not to use it unless you know what you’re doing”…

Well? I guess I do know what i’m doing? Hehehe… :laughing:

Thanks again!