def post(event): '''Place a new event on the queue. This places a new event at the end of the event queue. These Events will later be retrieved from the other queue functions. This is usually used for placing _pygame_USEREVENT events on the queue. Although any type of event can be placed, if using the sytem event types your program should be sure to create the standard attributes with appropriate values. :Parameters: `event` : Event Event to add to the queue. ''' global _user_event_nextid _pygame_display._video_init_check() sdl_event = SDL_Event(event.type) sdl_event.user.code = _USEROBJECT_CHECK1 sdl_event.user.data1 = c_void_p(_USEROBJECT_CHECK2) sdl_event.user.data2 = c_void_p(_user_event_nextid) _user_event_objects[_user_event_nextid] = event _user_event_nextid += 1 SDL_PushEvent(sdl_event)
def set_blocked(typelist): '''Control which events are allowed on the queue. The given event types are not allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to disable an event type multiple times. If None is passed as the argument, this has the opposite effect and none of the event types are allowed to be placed on the queue. :note: events posted with `post` will not be blocked. :Parameters: `typelist` : int or sequence of int or None Event type or list of event types to disallow. ''' _pygame_display._video_init_check() if typelist is None: SDL_EventState(SDL_ALLEVENTS, SDL_IGNORE) elif hasattr(typelist, '__len__'): for val in typelist: SDL_EventState(val, SDL_IGNORE) else: SDL_EventState(typelist, SDL_IGNORE)
def clear(typelist=None): '''Remove all events from the queue. Remove all events or events of a specific type from the queue. This has the same effect as `get` except nothing is returned. This can be slightly more effecient when clearing a full event queue. :Parameters: `typelist` : int or sequence of int Event type or list of event types to remove. ''' _pygame_display._video_init_check() if typelist is None: mask = SDL_ALLEVENTS else: if hasattr(typelist, '__len__'): mask = reduce(lambda a,b: a | SDL_EVENTMASK(b), typelist, 0) else: mask = int(typelist) SDL_PumpEvents() events = [] new_events = SDL_PeepEvents(1, SDL_GETEVENT, mask) while new_events: new_events = SDL_PeepEvents(1, SDL_GETEVENT, mask)
def peek(typelist=None): '''Test if event types are waiting on the queue. Returns true if there are any events of the given type waiting on the queue. If a sequence of event types is passed, this will return True if any of those events are on the queue. :Parameters: `typelist` : int or sequence of int Event type or list of event types to look for. :rtype: bool ''' _pygame_display._video_init_check() if typelist is None: mask = SDL_ALLEVENTS else: if hasattr(typelist, '__len__'): mask = reduce(lambda a,b: a | SDL_EVENTMASK(b), typelist, 0) else: mask = SDL_EVENTMASK(int(typelist)) SDL_PumpEvents() events = SDL_PeepEvents(1, SDL_PEEKEVENT, mask) if typelist is None: if events: return Event(0, sdl_event=events[0], keep_userdata=True) else: return Event(_pygame_locals.NOEVENT) # XXX deviation from pygame return len(events) > 0
def get_grab(): '''Test if the program is sharing input devices. Returns true when the input events are grabbed for this application. Use `set_grab` to control this state. :rtype: bool ''' _pygame_display._video_init_check() return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON
def wait(): '''Wait for a single event from the queue. Returns a single event from the queue. If the queue is empty this function will wait until one is created. While the program is waiting it will sleep in an idle state. This is important for programs that want to share the system with other applications. :rtype: Event ''' _pygame_display._video_init_check() return Event(0, sdl_event=SDL_WaitEventAndReturn())
def poll(): '''Get a single event from the queue. Returns a single event from the queue. If the event queue is empty an event of type _pygame_NOEVENT will be returned immediately. The returned event is removed from the queue. :rtype: Event ''' _pygame_display._video_init_check() event = SDL_PollEventAndReturn() if event: return Event(0, sdl_event=event, keep_userdata=True) else: return Event(_pygame_locals.NOEVENT)
def pump(): '''Internally process pygame event handlers. For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call _pygame_event.pump() to allow pygame to handle internal actions. This function is not necessary if your program is consistently processing events on the queue through the other _pygame_event functions. There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up. ''' _pygame_display._video_init_check() SDL_PumpEvents()
def set_grab(grab): '''Control the sharing of input devices with other applications. When your program runs in a windowed environment, it will share the mouse and keyboard devices with other applications that have focus. If your program sets the event grab to True, it will lock all input into your program. It is best to not always grab the input, since it prevents the user from doing other things on their system. :Parameters: - `grab`: bool ''' _pygame_display._video_init_check() if grab: SDL_WM_GrabInput(SDL_GRAB_ON) else: SDL_WM_GrabInput(SDL_GRAB_OFF)
def get_blocked(typelist): '''Test if a type of event is blocked from the queue. Returns true if the given event type is blocked from the queue. :Parameters: - `event_type`: int :rtype: int ''' _pygame_display._video_init_check() if typelist == None: return SDL_EventState(SDL_ALLEVENTS, SDL_QUERY) == SDL_ENABLE elif hasattr(typelist, '__len__'): # XXX undocumented behaviour for val in typelist: if SDL_EventState(val, SDL_QUERY) == SDL_ENABLE: return True return False else: return SDL_EventState(typelist, SDL_QUERY) == SDL_ENABLE
def get(typelist=None): '''Get events from the queue. _pygame_event.get(): return Eventlist _pygame_event.get(type): return Eventlist _pygame_event.get(typelist): return Eventlist This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue. If you are only taking specific events from the queue, be aware that the queue could eventually fill up with the events you are not interested. :Parameters: `typelist` : int or sequence of int Event type or list of event types that can be returned. :rtype: list of `Event` ''' _pygame_display._video_init_check() if typelist is None: mask = SDL_ALLEVENTS else: if hasattr(typelist, '__len__'): mask = reduce(lambda a,b: a | SDL_EVENTMASK(b), typelist, 0) else: mask = int(typelist) SDL_PumpEvents() events = [] new_events = SDL_PeepEvents(1, SDL_GETEVENT, mask) while new_events: events.append(Event(0, sdl_event=new_events[0])) new_events = SDL_PeepEvents(1, SDL_GETEVENT, mask) return events