Need advice on how to detect two keys being up at once.

I asked on the panda3d irc channel earlier about this but couldnt get it to work so i thought id finally ask here.

My problem is i have a ship that when you press a it strafes right and left when you press b but i need it to return to the center when both keys are up.

i have a task called playerMove and i put this in it:

        base.accept('a-repeat', ship.playerStrafe, extraArgs = [self, "left"] )
        base.accept('d-repeat', ship.playerStrafe, extraArgs = [self, "right"] )
        base.accept('a-up', ship.playerStrafe, extraArgs = [self, "center"])
        base.accept('d-up', ship.playerStrafe, extraArgs = [self, "center"])   

but when i try to detect when the a and d key are up it can only detect one of them at a time. i need them to both be detected in one frame.
Here is my playerStrafe code if you need it :

   def playerStrafe(self, dir):
        
        if self.strafeAmount != 0:
            self.ship.hide()
            self.copyShip.show()
        else:
            self.ship.show()
            self.copyShip.hide()
        
        if dir == "left":
            self.strafeCenter = 2
            if self.strafeAmount > -45:
                self.copyShip.setR(self.ship, -5)
                self.strafeAmount -= 5
            
            if self.strafeAmount == -45:    
                self.ship.setX(self.ship, -5)
            

            
        if dir == "right":
            self.strafeCenter = 2
            if self.strafeAmount < 45:
                self.copyShip.setR(self.ship, 5)
                self.strafeAmount += 5
            
            if self.strafeAmount == 45:           
                self.ship.setX(self.ship, 5)
        
        if dir == "center":
            if self.strafeCenter > 0:
                self.strafeCenter -= 1
           
            
            if self.strafeCenter == 0:

                if self.strafeAmount < 0:
                    self.copyShip.setR(self.copyShip, 1)
                    self.strafeAmount += 1
                    
                
                if self.strafeAmount > 0:
                    self.copyShip.setR(self.copyShip, -1) 
                    self.strafeAmount -= 1
                

the strafe center is set to 2 whenever the player presses a or d but its supposed to go back to 0 when the keys are up (decreased by 1 when each key is up)

Its kinda confusing, i know. But if you have any advice i would really appreciate it!

I haven’t executed this code, but this is the general idea:


class blah():


    self.accept('a',self.strafeLeft)
    self.accept('d',self.strafeRight)
    self.accept('a-up',self.noStrafeLeft)
    self.accept('d-up',self.noStrafeRight)


    taskMgr.add (self.strafeTask,'strafeTask')



    def strafeLeft(self):
        self.strafingLeft = 1


    def strafeRight(self):
        self.strafingRight = 1


    def noStrafeLeft(self):
        self.strafingLeft = 0


    def noStrafeRight(self):
        self.strafingRight = 0



    def strafeTask(self,task):
        if self.strafingLeft == 1:
            pass  #Strafing to the left code here

        if self.strafingRight == 1:
            pass  # strafing to the right code here

        if (self.strafingLeft == 0) and (self.strafingRight == 0):
            pass  #Not strafing, both keys are up!

        return task.cont

Hope this helps!

You’d want to use Keyboard Polling instead of events for this. From this topic you can copy this function by ynjh_jo:

  def getButton(key):
    if len(key)==1:
       kb=KeyboardButton.asciiKey(key)
    else:
       if key=='del': key='_del'
       if hasattr(KeyboardButton,key): # keyboard button
          kb=getattr(KeyboardButton,key)()
       else:                           # mouse button
          kb=getattr(MouseButton,key)()
    return base.mouseWatcherNode.isButtonDown(kb)

So to use this, fill out the following psuedo code and place it inside your task

    ...
    if getButton('a'):
       strafeLeft
    if getButton('d'):
       strafeRight
    if not getButton('a') and not getButton('d'):
       center
    ...