Key press event not registering?

Hello, i am really new at coding with panda3d so forgive me if i am coding wrong. I have been having this problem when trying to make a model move when the W key is pressed. I have the following code:

from direct.showbase.ShowBase import ShowBase
from direct.showbase.DirectObject import DirectObject

class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)

    self.scene = self.loader.loadModel("models/environment")
            

    self.scene.reparentTo(self.render)
            
    self.scene.setScale(0.25, 0.25, 0.25)
    self.scene.setPos(0, 0, 0)

    self.panda = self.loader.loadModel("models/panda")

    self.panda.reparentTo(self.render)

    self.panda.setScale(0.5, 0.5, 0.5)
    self.panda.setPos(5, 5, 10)

class Movement(DirectObject):
def __init__(self):
self.accept(‘w’, self.Move)
self.accept(‘w-repeat’, self.Move)
self.accept(‘w-up’, self.Move)

def Move(self):
    print('pressed w')

app = Game()
app.run()

I don’t have a function for moving currently added, but when i press w or hold it, it doesn’t print ‘pressed w’. I have been following the code from the keyboard support tutorial.
The console shows no errors, and i’m on the latest version as of now.
Any help and/or ideas to make it work and for me to be able to move the “panda model”? Everything else is working, i can see the models and i can move the camera around.

I recommend you to take a look at the examples that come with the engine. Or you can download them here - https://panda3d.org/download/panda3d-1.10.11/panda3d-1.10.11-samples.zip

Example: roaming-ralph has an implementation of what you are trying to achieve.

Where can i find the samples if i already installed the engine?

If you have installed SDK panda. In the root folder, then look for the samples folder.

I took the time and restored the formatting of your code. Your problem is that you forgot to create an instance of Movement.

from direct.showbase.ShowBase import ShowBase
from direct.showbase.DirectObject import DirectObject

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        scene = self.loader.loadModel("models/environment")
        scene.reparentTo(self.render)
        scene.setScale(0.25, 0.25, 0.25)
        scene.setPos(0, 0, 0)

        panda = self.loader.loadModel("models/panda")
        panda.reparentTo(self.render)
        panda.setScale(0.5, 0.5, 0.5)
        panda.setPos(5, 5, 10)

        movement = Movement()

class Movement(DirectObject):
    def __init__(self):
        self.accept('w', self.Move)
        self.accept('w-repeat', self.Move)
        self.accept('w-up', self.Move)

    def Move(self):
        print('pressed w')

app = Game()
app.run()
1 Like

Thanks a lot!