📄 sdlevent.html
字号:
<HTML><HEAD><TITLE>SDL_Event</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="SDL Library Documentation"HREF="index.html"><LINKREL="UP"TITLE="SDL Event Structures."HREF="eventstructures.html"><LINKREL="PREVIOUS"TITLE="SDL Event Structures."HREF="eventstructures.html"><LINKREL="NEXT"TITLE="SDL_ActiveEvent"HREF="sdlactiveevent.html"></HEAD><BODYCLASS="REFENTRY"BGCOLOR="#FFF8DC"TEXT="#000000"LINK="#0000ee"VLINK="#551a8b"ALINK="#ff0000"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">SDL Library Documentation</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="eventstructures.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sdlactiveevent.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="SDLEVENT"></A>SDL_Event</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN3711"></A><H2>Name</H2>SDL_Event -- General event structure</DIV><DIVCLASS="REFSECT1"><ANAME="AEN3714"></A><H2>Structure Definition</H2><PRECLASS="PROGRAMLISTING">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_SywWMEvent syswm;} SDL_Event;</PRE></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3717"></A><H2>Structure Data</H2><DIVCLASS="INFORMALTABLE"><ANAME="AEN3719"></A><P></P><TABLEBORDER="0"CLASS="CALSTABLE"><TBODY><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>type</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP">The type of event</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>active</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlactiveevent.html">Activation event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>key</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlkeyboardevent.html">Keyboard event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>motion</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlmousemotionevent.html">Mouse motion event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>button</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlmousebuttonevent.html">Mouse button event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>jaxis</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyaxisevent.html">Joystick axis motion event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>jball</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyballevent.html">Joystick trackball motion event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>jhat</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyhatevent.html">Joystick hat motion event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>jbutton</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoybuttonevent.html">Joystick button event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>resize</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlresizeevent.html">Application window resize event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>expose</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlexposeevent.html">Application window expose event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>quit</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlquitevent.html">Application quit request event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>user</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdluserevent.html">User defined event</A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="STRUCTFIELD"><I>syswm</I></TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlsyswmevent.html">Undefined window manager event</A></TD></TR></TBODY></TABLE><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3791"></A><H2>Description</H2><P>The <SPANCLASS="STRUCTNAME">SDL_Event</SPAN> union is the core to all event handling is SDL, its probably the most important structure after <SPANCLASS="STRUCTNAME">SDL_Surface</SPAN>. <SPANCLASS="STRUCTNAME">SDL_Event</SPAN> 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 <TTCLASS="STRUCTFIELD"><I>type</I></TT>.</P><P><DIVCLASS="INFORMALTABLE"><ANAME="AEN3799"></A><P></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THALIGN="LEFT"VALIGN="TOP">Event <TTCLASS="STRUCTFIELD"><I>type</I></TT></TH><THALIGN="LEFT"VALIGN="TOP">Event Structure</TH></TR></THEAD><TBODY><TR><TDALIGN="LEFT"VALIGN="MIDDLE"><TTCLASS="LITERAL">SDL_ACTIVEEVENT</TT></TD><TDALIGN="LEFT"VALIGN="MIDDLE"><AHREF="sdlactiveevent.html"><SPANCLASS="STRUCTNAME">SDL_ActiveEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_KEYDOWN/UP</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlkeyboardevent.html"><SPANCLASS="STRUCTNAME">SDL_KeyboardEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_MOUSEMOTION</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlmousemotionevent.html"><SPANCLASS="STRUCTNAME">SDL_MouseMotionEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_MOUSEBUTTONDOWN/UP</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlmousebuttonevent.html"><SPANCLASS="STRUCTNAME">SDL_MouseButtonEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_JOYAXISMOTION</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyaxisevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyAxisEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_JOYBALLMOTION</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyballevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyBallEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_JOYHATMOTION</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoyhatevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyHatEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_JOYBUTTONDOWN/UP</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdljoybuttonevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyButtonEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_QUIT</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlquitevent.html"><SPANCLASS="STRUCTNAME">SDL_QuitEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_SYSWMEVENT</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlsyswmevent.html"><SPANCLASS="STRUCTNAME">SDL_SysWMEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_VIDEORESIZE</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlresizeevent.html"><SPANCLASS="STRUCTNAME">SDL_ResizeEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_VIDEOEXPOSE</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdlexposeevent.html"><SPANCLASS="STRUCTNAME">SDL_ExposeEvent</SPAN></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="LITERAL">SDL_USEREVENT</TT></TD><TDALIGN="LEFT"VALIGN="TOP"><AHREF="sdluserevent.html"><SPANCLASS="STRUCTNAME">SDL_UserEvent</SPAN></A></TD></TR></TBODY></TABLE><P></P></DIV></P></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3885"></A><H2>Use</H2><P>The <SPANCLASS="STRUCTNAME">SDL_Event</SPAN> structure has two uses</P><P></P><ULCOMPACT="COMPACT"><LI><P>Reading events on the event queue</P></LI><LI><P>Placing events on the event queue</P></LI></UL><P>Reading events from the event queue is done with either <AHREF="sdlpollevent.html"><TTCLASS="FUNCTION">SDL_PollEvent</TT></A> or <AHREF="sdlpeepevents.html"><TTCLASS="FUNCTION">SDL_PeepEvents</TT></A>. We'll use <TTCLASS="FUNCTION">SDL_PollEvent</TT> and step through an example.</P><P>First off, we create an empty <SPANCLASS="STRUCTNAME">SDL_Event</SPAN> structure.<PRECLASS="PROGRAMLISTING">SDL_Event test_event;</PRE><TTCLASS="FUNCTION">SDL_PollEvent</TT> removes the next event from the event queue, if there are no events on the queue it returns <SPANCLASS="RETURNVALUE">0</SPAN> otherwise it returns <SPANCLASS="RETURNVALUE">1</SPAN>. We use a <TTCLASS="FUNCTION">while</TT> loop to process each event in turn.<PRECLASS="PROGRAMLISTING">while(SDL_PollEvent(&test_event)) {</PRE>The <TTCLASS="FUNCTION">SDL_PollEvent</TT> function take a pointer to an <SPANCLASS="STRUCTNAME">SDL_Event</SPAN> structure that is to be filled with event information. We know that if <TTCLASS="FUNCTION">SDL_PollEvent</TT> removes an event from the queue then the event information will be placed in our <SPANCLASS="STRUCTNAME">test_event</SPAN> structure, but we also know that the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">type</I></SPAN> of event will be placed in the <TTCLASS="STRUCTFIELD"><I>type</I></TT> member of <SPANCLASS="STRUCTNAME">test_event</SPAN>. So to handle each event <TTCLASS="STRUCTFIELD"><I>type</I></TT> seperately we use a <TTCLASS="FUNCTION">switch</TT> statement.<PRECLASS="PROGRAMLISTING"> switch(test_event.type) {</PRE>We need to know what kind of events we're looking for <SPANCLASS="emphasis"><ICLASS="EMPHASIS">and</I></SPAN> the event <TTCLASS="STRUCTFIELD"><I>type</I></TT>'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 <TTCLASS="LITERAL">SDL_MOUSEMOTION</TT> is, more than likely, the event we're looking for. A little <AHREF="sdlmousemotionevent.html">more</A> research tells use that <TTCLASS="LITERAL">SDL_MOUSEMOTION</TT> events are handled within the <AHREF="sdlmousemotionevent.html"><SPANCLASS="STRUCTNAME">SDL_MouseMotionEvent</SPAN></A> structure which is the <TTCLASS="STRUCTFIELD"><I>motion</I></TT> member of <SPANCLASS="STRUCTNAME">SDL_Event</SPAN>. We can check for the <TTCLASS="LITERAL">SDL_MOUSEMOTION</TT> event <TTCLASS="STRUCTFIELD"><I>type</I></TT> within our <TTCLASS="FUNCTION">switch</TT> statement like so:<PRECLASS="PROGRAMLISTING"> case SDL_MOUSEMOTION:</PRE>All we need do now is read the information out of the <TTCLASS="STRUCTFIELD"><I>motion</I></TT> member of <SPANCLASS="STRUCTNAME">test_event</SPAN>.<PRECLASS="PROGRAMLISTING"> printf("We got a motion event.\n"); printf("Current mouse position is: (%d, %d)\n", test_event.motion.x, test_event.motion.y); break; default: printf("Unhandled Event!\n"); break; }}printf("Event queue empty.\n");</PRE></P><P>It is also possible to push events onto the event queue and so use it as a two-way communication path. Both <AHREF="sdlpushevent.html"><TTCLASS="FUNCTION">SDL_PushEvent</TT></A> and <AHREF="sdlpeepevents.html"><TTCLASS="FUNCTION">SDL_PeepEvents</TT></A> allow you to place events onto the event queue. This is usually used to place a <TTCLASS="LITERAL">SDL_USEREVENT</TT> 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 <TTCLASS="STRUCTFIELD"><I>type</I></TT> member and filling the appropriate member structure with information.<PRECLASS="PROGRAMLISTING">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);</PRE></P></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3942"></A><H2>See Also</H2><P><AHREF="sdlpollevent.html"><TTCLASS="FUNCTION">SDL_PollEvent</TT></A>,<AHREF="sdlpushevent.html"><TTCLASS="FUNCTION">SDL_PushEvent</TT></A>,<AHREF="sdlpeepevents.html"><TTCLASS="FUNCTION">SDL_PeepEvents</TT></A></P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="eventstructures.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sdlactiveevent.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">SDL Event Structures.</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="eventstructures.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">SDL_ActiveEvent</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -