Event Order & Cancelling Events

Is it possible to set which order events are responded to or to cancel them?

For example, my game listens for ‘escape’ to toggle the in game menu and leave open menus.

I’d also like to have a Chat object that listens to ‘escape’ to cancel entering chat.

Currently, the escape event is handled by all direct objects that listen to it, so if you cancel chat, you also toggle the in game menu.

Is there anyway I can make it so that the chat object handles the escape event first and then cancels it? Or do I have to set it up so that the game knows that chat handle the event so that it doesn’t toggle the menu?

Edit:
I forgot that the game listens to other key presses (e.g. wasd), so I’m going to have to know when the user is typing so I can temporarily disable listening to the movement keys. I was hoping to make my chat objects completely self contained, but I guess I’ll have to live with a small amount of coupling.

Usually you would handle something like this using finite state machines. Just make a state for chatting, menu and game. This will make it easy to manage which input is enabled when.

With the FSM, in terms of design, would it make sense for the FSM to turn off the input listening or just be used to see what state the user is in.

e.g.

    def enterChat(self):
        self.ignore('w')
        self.ignore('s')
        self.ignore('a')
        self.ignore('d')
        self.ignore('w-up')
        self.ignore('s-up')
        self.ignore('a-up')
        self.ignore('d-up')
        
        self.ignore('space')
        self.ignore('space-up')
        self.ignore('q')
        
        self.ignore('escape')
        self.ignore('e')

    def exitChat(self):
        # re accept all the keyboard events

Versus

def UpdateMyKeysPressed(self):
    keys = []
        
    if(inputFSM.state == 'Game'):

        if (self.keyMap['KEY_FWD']):
            keys.append(Globals.KEY_FWD)
            
        elif (self.keyMap['KEY_BACK']):
            keys.append(Globals.KEY_BACK)
            
        if (self.keyMap['KEY_RIGHT']):
            keys.append(Globals.KEY_RIGHT)
            
        elif (self.keyMap['KEY_LEFT']):
            keys.append(Globals.KEY_LEFT)
            
        if (self.keyMap['KEY_JUMP']):
            keys.append(Globals.KEY_JUMP)
            
    return keys 

I ended up going with the former. Thanks for the help