pygame.event
pygame module for interacting with events and queues
pygame.event.pump internally process pygame event handlers
pygame.event.get get events from the queue
pygame.event.poll get a single event from the queue
pygame.event.wait wait for a single event from the queue
pygame.event.peek test if event types are waiting on the queue
pygame.event.clear remove all events from the queue
pygame.event.event_name get the string name from an event id
pygame.event.set_blocked control which events are allowed on the queue
pygame.event.set_allowed control which events are allowed on the queue
pygame.event.get_blocked test if a type of event is blocked from the queue
pygame.event.set_grab control the sharing of input devices with other applications
pygame.event.get_grab test if the program is sharing input devices
pygame.event.post place a new event on the queue
pygame.event.custom_type make custom user event type
pygame.event.Event create a new event object
pygame.event.EventType pygame object for representing events

Pygame handles all its event messaging through an event queue. The routines in this module help you manage that event queue. The input queue is heavily dependent on the pygame.displaypygame module to control the display window and screen module. If the display has not been initialized and a video mode not set, the event queue may not work properly. The event subsystem should be called from the main thread. If you want to post events into the queue from other threads, please use the pygame.fastevent module.

The event queue has an upper limit on the number of events it can hold (128 for standard SDL 1.2). When the queue becomes full new events are quietly dropped. To prevent lost events, especially input events which signal a quit command, your program must regularly check for events and process them. To speed up queue processing use pygame.event.set_blocked()control which events are allowed on the queue to limit which events get queued.

To get the state of various input devices, you can forego the event queue and access the input devices directly with their appropriate modules: pygame.mousepygame module to work with the mouse, pygame.keypygame module to work with the keyboard, and pygame.joystickPygame module for interacting with joysticks, gamepads, and trackballs.. If you use this method, remember that pygame requires some form of communication with the system window manager and other parts of the platform. To keep pygame in sync with the system, you will need to call pygame.event.pump()internally process pygame event handlers to keep everything current. Usually, this should be called once per game loop. Note: Joysticks will not send any events until the device has been initialized.

The event queue contains pygame.event.EventTypepygame object for representing events event objects. There are a variety of ways to access the queued events, from simply checking for the existence of events, to grabbing them directly off the stack. The event queue also offers some simple filtering which can slightly help performance by blocking certain event types from the queue. Use pygame.event.set_allowed()control which events are allowed on the queue and pygame.event.set_blocked()control which events are allowed on the queue to change this filtering. By default, all event types can be placed on the queue.

All pygame.event.EventTypepygame object for representing events instances contain an event type identifier and attributes specific to that event type. The event type identifier is accessible as the pygame.event.EventType.typeevent type identifier. property. Any of the event specific attributes can be accessed through the pygame.event.EventType.__dict__event attribute dictionary attribute or directly as an attribute of the event object (as member lookups are passed through to the object's dictionary values). The event object has no method functions. Users can create their own new events with the pygame.event.Event()create a new event object function.

The event type identifier is in between the values of NOEVENT and NUMEVENTS. User defined events should have a value in the inclusive range of USEREVENT to NUMEVENTS - 1. It is recommended all user events follow this system.

Events support equality and inequality comparisons. Two events are equal if they are the same type and have identical attribute values.

While debugging and experimenting, you can print an event object for a quick display of its type and members. The function pygame.event.event_name()get the string name from an event id can be used to get a string representing the name of the event type.

Events that come from the system will have a guaranteed set of member attributes based on the type. The following is a list event types with their specific attributes.

QUIT              none
ACTIVEEVENT       gain, state
KEYDOWN           key, mod, unicode, scancode
KEYUP             key, mod
MOUSEMOTION       pos, rel, buttons
MOUSEBUTTONUP     pos, button
MOUSEBUTTONDOWN   pos, button
JOYAXISMOTION     joy, axis, value
JOYBALLMOTION     joy, ball, rel
JOYHATMOTION      joy, hat, value
JOYBUTTONUP       joy, button
JOYBUTTONDOWN     joy, button
VIDEORESIZE       size, w, h
VIDEOEXPOSE       none
USEREVENT         code

New in pygame 1.9.2.

On MacOSX when a file is opened using a pygame application, a USEREVENT with its code attribute set to pygame.USEREVENT_DROPFILE is generated. There is an additional attribute called filename where the name of the file being accessed is stored.

USEREVENT         code=pygame.USEREVENT_DROPFILE, filename

New in pygame 1.9.5.

When compiled with SDL2, pygame has these additional events and their attributes.

AUDIODEVICEADDED   which, iscapture
AUDIODEVICEREMOVED which, iscapture
FINGERMOTION       touch_id, finger_id, x, y, dx, dy
FINGERDOWN         touch_id, finger_id, x, y, dx, dy
FINGERUP           touch_id, finger_id, x, y, dx, dy
MULTIGESTURE       touch_id, x, y, pinched, rotated, num_fingers
TEXTEDITING        text, start, length
TEXTINPUT          text

New in pygame 2.0.0.

pygame can recognize text or files dropped in its window. If a file is dropped, file will be its path. The DROPTEXT event is only supported on X11.

DROPBEGIN
DROPCOMPLETE
DROPFILE        file
DROPTEXT        text

New in pygame 2.0.0.

Events reserved for pygame.midipygame module for interacting with midi input and output. use.

MIDIIN
MIDIOUT

pygame.event.pump()
internally process pygame event handlers
pump() -> None

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.eventpygame module for interacting with events and queues 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.

Caution

This function should only be called in the thread that initialized pygame.displaypygame module to control the display window and screen.

pygame.event.get()
get events from the queue
get(eventtype=None) -> Eventlist
get(eventtype=None, pump=True) -> 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.

If pump is True (the default), then pygame.event.pump()internally process pygame event handlers will be called.

New in pygame 1.9.5: pump

pygame.event.poll()
get a single event from the queue
poll() -> EventType instance

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.

Caution

This function should only be called in the thread that initialized pygame.displaypygame module to control the display window and screen.

pygame.event.wait()
wait for a single event from the queue
wait() -> EventType instance

Returns a single event from the queue. If the queue is empty this function will wait until one is created. The event is removed from the queue once it has been returned. 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.

Caution

This function should only be called in the thread that initialized pygame.displaypygame module to control the display window and screen.

pygame.event.peek()
test if event types are waiting on the queue
peek(eventtype=None) -> bool
peek(eventtype=None, pump=True) -> bool

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.

If pump is True (the default), then pygame.event.pump()internally process pygame event handlers will be called.

New in pygame 1.9.5: pump

pygame.event.clear()
remove all events from the queue
clear(eventtype=None) -> None
clear(eventtype=None, pump=True) -> None

Removes all events from the queue. If eventtype is given, removes the given event or sequence of events. This has the same effect as pygame.event.get()get events from the queue except None is returned. It can be slightly more efficient when clearing a full event queue.

If pump is True (the default), then pygame.event.pump()internally process pygame event handlers will be called.

New in pygame 1.9.5: pump

pygame.event.event_name()
get the string name from an event id
event_name(type) -> string

Returns a string representing the name (in CapWords style) of the given event type.

"UserEvent" is returned for all values in the user event id range. "Unknown" is returned when the event type does not exist.

pygame.event.set_blocked()
control which events are allowed on the queue
set_blocked(type) -> None
set_blocked(typelist) -> None
set_blocked(None) -> None

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, ALL of the event types are blocked from being placed on the queue.

pygame.event.set_allowed()
control which events are allowed on the queue
set_allowed(type) -> None
set_allowed(typelist) -> None
set_allowed(None) -> None

The given event types are allowed to appear on the event queue. By default, all event types can be placed on the queue. It is safe to enable an event type multiple times.

If None is passed as the argument, ALL of the event types are allowed to be placed on the queue.

pygame.event.get_blocked()
test if a type of event is blocked from the queue
get_blocked(type) -> bool
get_blocked(typelist) -> bool

Returns True if the given event type is blocked from the queue. If a sequence of event types is passed, this will return True if any of those event types are blocked.

pygame.event.set_grab()
control the sharing of input devices with other applications
set_grab(bool) -> None

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.

pygame.event.get_grab()
test if the program is sharing input devices
get_grab() -> bool

Returns True when the input events are grabbed for this application.

pygame.event.post()
place a new event on the queue
post(Event) -> None

Places the given event at the end of the event queue.

This is usually used for placing pygame.USEREVENT events on the queue. Although any type of event can be placed, if using the system event types your program should be sure to create the standard attributes with appropriate values.

If the event queue is full a pygame.errorstandard pygame exception is raised.

pygame.event.custom_type()
make custom user event type
custom_type() -> int

Reserves a pygame.USEREVENT for a custom use.

If too many events are made a pygame.errorstandard pygame exception is raised.

New in pygame 2.0.0.dev3.

pygame.event.Event()
create a new event object
Event(type, dict) -> EventType instance
Event(type, **attributes) -> EventType instance

Creates a new event with the given type and attributes. The attributes can come from a dictionary argument with string keys or from keyword arguments.

pygame.event.EventType
pygame object for representing events
pygame.event.EventType.type event type identifier.
pygame.event.EventType.__dict__ event attribute dictionary

A pygame object that represents an event. User event instances are created with an pygame.event.Event()create a new event object function call. The EventType type is not directly callable. EventType instances support attribute assignment and deletion.

type
event type identifier.
type -> int

Read-only. The event type identifier. For user created event objects, this is the type argument passed to pygame.event.Event()create a new event object.

For example, some predefined event identifiers are QUIT and MOUSEMOTION.

__dict__
event attribute dictionary
__dict__ -> dict

Read-only. The event type specific attributes of an event. The dict attribute is a synonym for backward compatibility.

For example, the attributes of a KEYDOWN event would be unicode, key, and mod

New in pygame 1.9.2: Mutable attributes.




Edit on GitHub