SDL_Event



SDL_Event

NAME
STRUCTURE DEFINITION
STRUCTURE DATA
DESCRIPTION
USE
SEE ALSO

NAME

SDL_Event − General event structure

STRUCTURE DEFINITION

typedef union{
  Uint8 type;
  SDL_ActiveEvent active;
  SDL_KeyboardEvent key;
  SDL_MouseMotionEvent motion;
  SDL_MouseButtonEvent button;
  SDL_JoyAxisEvent jaxis;
  SDL_JoyBallEvent jball;
  SDL_JoyHatEvent jhat;
  SDL_JoyButtonEvent jbutton;
  SDL_ResizeEvent resize;
  SDL_ExposeEvent expose;
  SDL_QuitEvent quit;
  SDL_UserEvent user;
  SDL_SysWMEvent syswm;
} SDL_Event;

STRUCTURE DATA

type

The type of event

active

Activation event

key

Keyboard event

motion

Mouse motion event

button

Mouse button event

jaxis

Joystick axis motion event

jball

Joystick trackball motion event

jhat

Joystick hat motion event

jbutton

Joystick button event

resize

Application window resize event

expose

Application window expose event

quit

Application quit request event

user

User defined event

syswm

Undefined window manager event

DESCRIPTION

The SDL_Event union is the core to all event handling is SDL, its probably the most important structure after SDL_Surface. SDL_Event is a union of all event structures used in SDL, using it is a simple matter of knowing which union member relates to which event type.

Event type

Event Structure

SDL_ACTIVEEVENT

SDL_ActiveEvent

SDL_KEYDOWN/UP

SDL_KeyboardEvent

SDL_MOUSEMOTION

SDL_MouseMotionEvent

SDL_MOUSEBUTTONDOWN/UP

SDL_MouseButtonEvent

SDL_JOYAXISMOTION

SDL_JoyAxisEvent

SDL_JOYBALLMOTION

SDL_JoyBallEvent

SDL_JOYHATMOTION

SDL_JoyHatEvent

SDL_JOYBUTTONDOWN/UP

SDL_JoyButtonEvent

SDL_QUIT

SDL_QuitEvent

SDL_SYSWMEVENT

SDL_SysWMEvent

SDL_VIDEORESIZE

SDL_ResizeEvent

SDL_VIDEOEXPOSE

SDL_ExposeEvent

SDL_USEREVENT

SDL_UserEvent

USE

The SDL_Event structure has two uses

Reading events on the event queue

Placing events on the event queue

Reading events from the event queue is done with either SDL_PollEvent or SDL_PeepEvents. We’ll use SDL_PollEvent and step through an example.

First off, we create an empty SDL_Event structure.

SDL_Event test_event;

SDL_PollEvent removes the next event from the event queue, if there are no events on the queue it returns 0 otherwise it returns 1. We use a while loop to process each event in turn.

while(SDL_PollEvent(&test_event)) {

The SDL_PollEvent function take a pointer to an SDL_Event structure that is to be filled with event information. We know that if SDL_PollEvent removes an event from the queue then the event information will be placed in our test_event structure, but we also know that the type of event will be placed in the type member of test_event. So to handle each event type seperately we use a switch statement.

  switch(test_event.type) {

We need to know what kind of events we’re looking for and the event type’s of those events. So lets assume we want to detect where the user is moving the mouse pointer within our application. We look through our event types and notice that SDL_MOUSEMOTION is, more than likely, the event we’re looking for. A little more research tells use that SDL_MOUSEMOTION events are handled within the SDL_MouseMotionEvent structure which is the motion member of SDL_Event. We can check for the SDL_MOUSEMOTION event type within our switch statement like so:

    case SDL_MOUSEMOTION:

All we need do now is read the information out of the motion member of test_event.

      printf("We got a motion event.
");
      printf("Current mouse position is: (%d, %d)
", test_event.motion.x, test_event.motion.y);
      break;
    default:
      printf("Unhandled Event!
");
      break;
  }
}
printf("Event queue empty.
");

It is also possible to push events onto the event queue and so use it as a two-way communication path. Both SDL_PushEvent and SDL_PeepEvents allow you to place events onto the event queue. This is usually used to place a SDL_USEREVENT on the event queue, however you could use it to post fake input events if you wished. Creating your own events is a simple matter of choosing the event type you want, setting the type member and filling the appropriate member structure with information.

SDL_Event user_event;


user_event.type=SDL_USEREVENT;
user_event.user.code=2;
user_event.user.data1=NULL;
user_event.user.data2=NULL;
SDL_PushEvent(&user_event);

SEE ALSO

SDL_PollEvent, SDL_PushEvent, SDL_PeepEvents



More Linux Commands

manpages/svnsync.1.html
svnsync(1) - Subversion repository synchronization tool.....
svnsync.1 - Subversion is a version control system, which allows you to keep old versions of files and directories (usually source code), keep a log of who, whe

manpages/id3v2.1.html
id3v2(1) (Commands - Linux manual page)....................
Display version information, genres, help, frames, tags, album title track, artist, song, comment, year, id3v2 - Adds/Modifies/Removes/Views id3v2, converts...

manpages/dhclient.8.html
dhclient(8) - Dynamic Host Configuration Protocol Client....
The Internet Systems Consortium DHCP Client, dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Prot

manpages/column.1.html
column(1) - columnate lists (Commands - Linux man page).....
The column utility formats its input into multiple columns. By default, rows are filled before columns. Input is taken from file, or otherwise from standard inp

manpages/console.n.html
console(n) - Control the console on systems without a real c
The console window is a replacement for a real console to allow input and output on the standard I/O channels on platforms that do not have a real console. It i

manpages/FcDirCacheValid.3.html
FcDirCacheValid(3) - check directory cache - Linux man page
Returns FcTrue if dir has an associated valid cache file, else returns FcFalse VERSION Fontconfig version 2.8.0 FcDirCacheValid.3 (Library - Linux manual page)

manpages/lzgrep.1.html
lzgrep(1) - search compressed files for a regular expression
xzgrep invokes grep(1) on files which may be either uncompressed or compressed with xz(1), lzma(1), gzip(1), or bzip2(1). All options specified are passed direc

manpages/removexattr.2.html
removexattr(2) - remove an extended attribute (Man Page)....
Extended attributes are name:value pairs associated with inodes (files, directories, symbolic links, etc.). They are extensions to the normal attributes which a

manpages/fmodf.3.html
fmodf(3) - floating-point remainder function (Man Page).....
The fmod() function computes the floating-point remainder of dividing x by y. The return value is x - n * y, where n is the quotient of x / y, rounded toward ze

manpages/Tcl_ExprLong.3.html
Tcl_ExprLong(3) - evaluate an expression - Linux man page...
These four procedures all evaluate the expression given by the expr argument and return the result in one of four different forms. The expression can have any o

manpages/MIME::Decoder::Base64.3pm.html
MIME::Decoder::Base64(3pm) - encode/decode a "base64" stream
A MIME::Decoder subclass for the base64 encoding. The name was chosen to jibe with the pre-existing MIME::Base64 utility package, which this class actually uses

manpages/XtAllocateGC.3.html
XtAllocateGC(3) - obtain a sharable GC with modifiable field
The XtAllocateGC function returns a sharable GC that may be modified by the client. The screen field of the specified widget or of the nearest widget ancestor o





We can't live, work or learn in freedom unless the software we use is free.