holding down a key?


Just today I started coding with panda3d and it looks great.

I managed to do some stuff from the tutorial. Now I have the environment, the panda, which runs in circles, and I managed to control the camera angle by moving the mouse.

Now I want to control the position of the camera using the w a s d keys. and it works, but it does not work when I hold they key.

what I do is:
self.accept(“w”,self.setButtn,[“w”]) to catch the key. the setButtn fuction just changes the value of a variable.

then, in the moveCam task, I look whats in the variable, and do one of 4 things, like this:

s = 0.1
if self.button=="w":
	pos = self.camera.getPos()
	h = self.camera.getHpr()[0]+90
	p = self.camera.getHpr()[1]
	h_R = h*pi/180
	p_R = p*pi/180
	dx = s*cos(h_R)
	dy = s*sin(h_R)

But that only works if I keep pressing the key, it does not work when I just hold the key pressed.

How do I need to modify it to have it run with holding a key pressed? and will this work if two keys are pressed at the same time?

You need to capture the key down and key up events. For example:



Avoid using sine and cosine. Instead, use relative positioning, it’s a lot easier. You could just do in a task:

if base.mouseWatcherNode.isButtonDown(KeyboardButton.asciiKey("w")):
  self.camera.setY(self.camera, speed * globalClock.getDt())

This will move the camera forward relative to its own coordinate system as long as you hold “w”.