there any form to work with listener for events in python or in bge?

I was see this

http://www.blender.org/documentation/blender_python_api_2_67_release/bge.events.html

instead of to write this


if keyboard.events[bge.events.WKEY] == JUST_ACTIVATED:
  character.move()

I want to do with listener and events


character.addListener(...

is it possible?

Of course it is!


from collections import defaultdict
from functools import partial
from bge import logic, events


class EventManager:
    
    def __init__(self):
        self.events = defaultdict(set)
    
    def add_listener(self, event, status, callback):
        self.events[event].add((status, callback))
    
    def update(self):
        events = logic.keyboard.active_events.copy()
        events.update(logic.mouse.active_events)
        
        for event, data in self.events.items():
            for (status, callback) in data:
                if events.get(event) == status:
                    callback()


def move_obj(obj):
    obj.worldPosition.y += 0.1


def main(cont):
    own = cont.owner
    
    try:
        ev = own['ev']
    except KeyError:
        ev = own['ev'] = EventManager()           
        
        ev.add_listener(events.WKEY, logic.KX_INPUT_ACTIVE, partial(move_obj, own))
        ev.add_listener(events.PKEY, logic.KX_INPUT_JUST_ACTIVATED, partial(print, own))
    
    ev.update()                 

Great! Thanks