Event Handling¶
Event handling allows your application to receive input from the user. Event handling is initialized with a call to:
>>> from csdl2 import *
>>> SDL_Init(SDL_INIT_EVENTS)
SDL stores each event as a SDL_Event in an event queue. SDL_Event structures are read from the queue with the SDL_PollEvent function and it is then up to the application to process the information stored with them.
-
class
csdl2.SDL_Event¶ A union that contains structures for the different event types.
-
type¶ An int specifying the event type. Use the event type’s corresponding attribute to get/set information about the event:
Value of typeAttr of SDL_EventSDL_CONTROLLERAXISMOTIONSDL_Event.caxisSDL_CONTROLLERBUTTONDOWN,SDL_CONTROLLERBUTTONUPSDL_Event.cbuttonSDL_CONTROLLERDEVICEADDED,SDL_CONTROLLERDEVICEREMOVED,SDL_CONTROLLERDEVICEREMAPPEDSDL_Event.cdeviceSDL_DOLLARGESTURE,SDL_DOLLARRECORDSDL_Event.dgestureSDL_DROPFILESDL_Event.dropSDL_FINGERMOTION,SDL_FINGERDOWN,SDL_FINGERUPSDL_Event.tfingerSDL_KEYDOWN,SDL_KEYUPSDL_Event.keySDL_JOYAXISMOTIONSDL_Event.jaxisSDL_JOYBALLMOTIONSDL_Event.jballSDL_JOYHATMOTIONSDL_Event.jhatSDL_JOYBUTTONDOWN,SDL_JOYBUTTONUPSDL_Event.jbuttonSDL_JOYDEVICEADDED,SDL_JOYDEVICEREMOVEDSDL_Event.jdeviceSDL_MOUSEMOTIONSDL_Event.motionSDL_MOUSEBUTTONDOWN,SDL_MOUSEBUTTONUPSDL_Event.buttonSDL_MOUSEWHEELSDL_Event.wheelSDL_MULTIGESTURESDL_Event.mgestureSDL_QUITSDL_Event.quitSDL_SYSWMEVENTSDL_Event.syswmSDL_TEXTEDITINGSDL_Event.editSDL_TEXTINPUTSDL_Event.textSDL_USEREVENTSDL_Event.userSDL_WINDOWEVENTSDL_Event.window
-
motion¶ (readonly) If
SDL_Event.typeisSDL_MOUSEMOTION, use this attribute to access the underlyingSDL_MouseMotionEventmouse motion event data.
-
-
csdl2.SDL_QUIT¶ User-requested quit.
-
csdl2.SDL_APP_TERMINATING¶ The application is being terminated by the OS. Called on iOS in applicationWillTerminate(). Called on Android in onDestroy().
-
csdl2.SDL_APP_LOWMEMORY¶ The application is low on memory, free memory if possible. Called on iOS in applicationDidReceiveMemoryWarning(). Called on Android in onLowMemory().
-
csdl2.SDL_APP_WILLENTERBACKGROUND¶ The application is about to enter the background. Called on iOS in applicationWillResignActive(). Called on Android in onPause().
-
csdl2.SDL_APP_DIDENTERBACKGROUND¶ The application did enter the background and may not get CPU for some time. Called on iOS in applicationDidEnterBackground(). Called on Android in onPause().
-
csdl2.SDL_APP_WILLENTERFOREGROUND¶ The application is about to enter the foreground. Called on iOS in applicationWillEnterForeground(). Called on Android in onResume().
-
csdl2.SDL_APP_DIDENTERFOREGROUND¶ The application is now interactive. Called on iOS in applicationDidBecomeActive(). Called on Android in onResume().
-
csdl2.SDL_WINDOWEVENT¶ Window state change.
-
csdl2.SDL_SYSWMEVENT¶ System specific event.
-
csdl2.SDL_KEYDOWN¶ Key pressed.
-
csdl2.SDL_KEYUP¶ Key released.
-
csdl2.SDL_TEXTEDITING¶ Keyboard text editing (composition).
-
csdl2.SDL_TEXTINPUT¶ Keyboard text input.
-
csdl2.SDL_MOUSEMOTION¶ Mouse moved.
-
csdl2.SDL_MOUSEBUTTONDOWN¶ Mouse button pressed.
-
csdl2.SDL_MOUSEBUTTONUP¶ Mouse button released.
-
csdl2.SDL_MOUSEWHEEL¶ Mouse wheel motion.
-
csdl2.SDL_JOYAXISMOTION¶ Joystick axis motion.
-
csdl2.SDL_JOYBALLMOTION¶ Joystick trackball motion.
-
csdl2.SDL_JOYHATMOTION¶ Joystick hat position change.
-
csdl2.SDL_JOYBUTTONDOWN¶ Joystick button pressed.
-
csdl2.SDL_JOYBUTTONUP¶ Joystick button released.
-
csdl2.SDL_JOYDEVICEADDED¶ A new joystick has been inserted into the system.
-
csdl2.SDL_JOYDEVICEREMOVED¶ An opened joystick has been removed.
-
csdl2.SDL_CONTROLLERAXISMOTION¶ Game controller axis motion.
-
csdl2.SDL_CONTROLLERBUTTONDOWN¶ Game controller button pressed.
-
csdl2.SDL_CONTROLLERBUTTONUP¶ Game controller button released.
-
csdl2.SDL_CONTROLLERDEVICEADDED¶ A new game controller has been inserted into the system.
-
csdl2.SDL_CONTROLLERDEVICEREMOVED¶ A opened game controller has been removed.
-
csdl2.SDL_CONTROLLERDEVICEREMAPPED¶ The controller mapping was updated.
-
csdl2.SDL_FINGERDOWN¶ User has touched input device.
-
csdl2.SDL_FINGERUP¶ User stopped touching input device.
-
csdl2.SDL_FINGERMOTION¶ User is dragging finger on input device.
-
csdl2.SDL_DOLLARGESTURE¶ User made a dollar gesture.
-
csdl2.SDL_DOLLARRECORD¶ When recording a gesture with SDL_RecordGesture, the user made a dollar gesture that was recorded.
-
csdl2.SDL_MULTIGESTURE¶ User made a gesture with multiple fingers.
-
csdl2.SDL_CLIPBOARDUPDATE¶ The clipboard changed.
-
csdl2.SDL_DROPFILE¶ The system requests a file open.
-
csdl2.SDL_USEREVENT¶ -
csdl2.SDL_LASTEVENT¶ Events
SDL_USEREVENTthroughSDL_LASTEVENTare for your use, and should be allocated with SDL_RegisterEvents
-
csdl2.SDL_PumpEvents() → None¶ Pumps the event loop, gathering events from the input devices.
This function updates the event queue and internal input device state. Without calling this function, no input events will ever be placed on the queue.
SDL_PollEvent()andSDL_WaitEvent()implicitly call this function. If you are not polling or waiting for events using these functions, you must explicitly callSDL_PumpEvents()to force an event queue update.This should only be run in the thread that sets the video mode.
-
csdl2.SDL_PeepEvents(events, numevents: int, action: int, minType: int, maxType: int) → int¶ If action is
SDL_ADDEVENT, up to numevents events will be added to the back of the event queue. Returns the number of events added.If action is
SDL_PEEKEVENT, up to numevents events from the front of the event queue, within minType and maxType, will be returned in events, but will not be removed from the queue. Returns number of events peeked.If action is
SDL_GETEVENT, up to numevents events from the front of the event queue, within minType and maxType, will be returned in events, and will be removed from the queue. Returns number of events retrieved.Parameters: - events (SDL_Event) – Either a
SDL_Eventobject, or a buffer of equivalent size. - numevents (int) – If action is
SDL_ADDEVENT, the number of events to add to the event queue. If action isSDL_PEEKEVENTorSDL_GETEVENT, the maximum number of events to retrieve. - action (int) – One of
SDL_ADDEVENT,SDL_PEEKEVENTorSDL_GETEVENT. - minType (int) – minimum value of the event type to be considered.
SDL_FIRSTEVENTis a safe choice. - maxType (int) – maximum value of the event type to be considered.
SDL_LASTEVENTis a safe choice.
Returns: Number of events added to the event queue for
SDL_ADDEVENT, number of events retrieved from the event queue forSDL_PEEKEVENTandSDL_GETEVENT.- events (SDL_Event) – Either a
-
csdl2.SDL_ADDEVENT¶ -
csdl2.SDL_PEEKEVENT¶ -
csdl2.SDL_GETEVENT¶ Possible actions for
SDL_PeepEvents().
-
csdl2.SDL_FlushEvents(minType: int, maxType: int) → None¶ Removes all events from the event queue within the specified minType and maxType.
To clear all events, set minType to
SDL_FIRSTEVENTand maxType toSDL_LASTEVENT. To clear all user events, set minType toSDL_USEREVENTand maxType toSDL_LASTEVENT.This function only affects currently queued events. If you wish to make sure that all pending OS events are flushed, you can call
SDL_PumpEvents()on the main thread immediately beforeSDL_FlushEvents().Parameters: - minType (int) – minimum event type to be cleared.
- maxType (int) – maximum event type to be cleared.
-
csdl2.SDL_PollEvent(event) → bool¶ Polls for currently pending events.
Parameters: event (SDL_Event or None) – If not None, the next event is removed from the queue and stored in it. If None, no event will be removed from the queue. Returns: True if there are events in the queue, False otherwise.
-
csdl2.SDL_PushEvent(event) → bool¶ Copies event into the event queue.
Parameters: event (SDL_Event) – Event to be copied into the event queue. Either a SDL_Eventinstance, or a buffer of equivalent size.Returns: True on success, False if the event was filtered. Note
For pushing application-specific events, please use
SDL_RegisterEvents()to get an event type that does not conflict with other code that also wants its own custom event types.
Mouse motion events¶
A SDL_MOUSEMOTION event occurs whenever a user moves the mouse within
any window or when SDL_WarpMouseInWindow() is called.
-
class
csdl2.SDL_MouseMotionEvent¶ A structure that contains mouse motion event information.
SDL_MouseMotionEventis a member of theSDL_Eventunion and is used when an event of typeSDL_MOUSEMOTIONis reported. You would access it through theSDL_Event.motionattribute.-
type¶ The event type. This should be
SDL_MOUSEMOTION.
-
timestamp¶ Timestamp of the event.
-
windowID¶ The window with mouse focus, if any.
-
which¶ The mouse instance ID. This may be
SDL_TOUCH_MOUSEID, for events that were generated by a touch input device, and not a real mouse. You might want to ignore such events, if your application already handles theSDL_FINGERMOTIONevent.
-
state¶ A 32-bit bitmask of the current button state and is the same as that returned by
SDL_GetMouseState(). You can test different buttons by using the masksSDL_BUTTON_LMASK,SDL_BUTTON_MMASK,SDL_BUTTON_RMASK,SDL_BUTTON_X1MASKandSDL_BUTTON_X2MASK.
-
x¶ X coordinate, relative to window.
-
y¶ Y coordinate, relative to window.
-
xrel¶ Motion in the X direction, relative to the last
SDL_MOUSEMOTIONevent. If relative mouse mode is enabled withSDL_SetRelativeMouseMode(), relative movement will still be reported even when the cursor reached the edge of the screen.
-
yrel¶ Motion in the Y direction, relative to the last
SDL_MOUSEMOTIONevent. If relative mouse mode is enabled withSDL_SetRelativeMouseMode(), relative movement will still be reported even when the cursor reached the edge of the screen.
-