⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guideinputkeyboard.html

📁 VC5.6.7的一个扩展库。跟DirectX的功能差不多。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Handling the Keyboard</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="SDL Library Documentation"HREF="index.html"><LINKREL="UP"TITLE="Input handling"HREF="guideinput.html"><LINKREL="PREVIOUS"TITLE="Input handling"HREF="guideinput.html"><LINKREL="NEXT"TITLE="Examples"HREF="guideexamples.html"></HEAD><BODYCLASS="SECT1"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="guideinput.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 3. Input handling</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="guideexamples.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GUIDEINPUTKEYBOARD"></A>Handling the Keyboard</H1><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN271"></A>Keyboard Related Structures</H2><P>It should make it a lot easier to understand this tutorial is you are familiar with the data types involved in keyboard access, so I'll explain them first.</P><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN274"></A>SDLKey</H3><P><SPANCLASS="STRUCTNAME">SDLKey</SPAN> is an enumerated type defined in SDL/include/SDL_keysym.h and detailed <AHREF="sdlkey.html">here</A>. Each <SPANCLASS="STRUCTNAME">SDLKey</SPAN> symbol represents a key, <TTCLASS="LITERAL">SDLK_a</TT> corresponds to the 'a' key on a keyboard, <TTCLASS="LITERAL">SDLK_SPACE</TT> corresponds to the space bar, and so on.</P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN282"></A>SDLMod</H3><P>SDLMod is an enumerated type, similar to <SPANCLASS="STRUCTNAME">SDLKey</SPAN>, however it enumerates keyboard modifiers (Control, Alt, Shift). The full list of modifier symbols is <AHREF="sdlkey.html#SDLMOD">here</A>. <SPANCLASS="STRUCTNAME">SDLMod</SPAN> values can be AND'd together to represent several modifiers.</P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN288"></A>SDL_keysym</H3><PRECLASS="PROGRAMLISTING">typedef struct{  Uint8 scancode;  SDLKey sym;  SDLMod mod;  Uint16 unicode;} SDL_keysym;</PRE><P>The <SPANCLASS="STRUCTNAME">SDL_keysym</SPAN> structure describes a key press or a key release. The <TTCLASS="STRUCTFIELD"><I>scancode</I></TT> field is hardware specific and should be ignored unless you know what your doing. The <TTCLASS="STRUCTFIELD"><I>sym</I></TT> field is the <SPANCLASS="STRUCTNAME">SDLKey</SPAN> value of the key being pressed or released. The <TTCLASS="STRUCTFIELD"><I>mod</I></TT> field describes the state of the keyboard modifiers at the time the key press or release occurred. So a value of <TTCLASS="LITERAL">KMOD_NUM | KMOD_CAPS | KMOD_LSHIFT</TT> would mean that Numlock, Capslock and the left shift key were all press (or enabled in the case of the lock keys). Finally, the <TTCLASS="STRUCTFIELD"><I>unicode</I></TT> field stores the 16-bit unicode value of the key.</P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>It should be noted and understood that this field is only valid when the <SPANCLASS="STRUCTNAME">SDL_keysym</SPAN> is describing a key press, not a key release. Unicode values only make sense on a key press because the unicode value describes an international character and only key presses produce characters. More information on Unicode can be found at <AHREF="http://www.unicode.org"TARGET="_top">www.unicode.org</A></P></BLOCKQUOTE></DIV><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>Unicode translation must be enabled using the <AHREF="sdlenableunicode.html"><TTCLASS="FUNCTION">SDL_EnableUNICODE</TT></A> function.</P></BLOCKQUOTE></DIV></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN307"></A>SDL_KeyboardEvent</H3><PRECLASS="PROGRAMLISTING">typedef struct{  Uint8 type;  Uint8 state;  SDL_keysym keysym;} SDL_KeyboardEvent;</PRE><P>The <SPANCLASS="STRUCTNAME">SDL_KeyboardEvent</SPAN> describes a keyboard event (obviously). The <TTCLASS="STRUCTFIELD"><I>key</I></TT> member of the <AHREF="sdlevent.html"><SPANCLASS="STRUCTNAME">SDL_Event</SPAN></A> union is a <SPANCLASS="STRUCTNAME">SDL_KeyboardEvent</SPAN> structure. The <TTCLASS="STRUCTFIELD"><I>type</I></TT> field specifies whether the event is a key release (<TTCLASS="LITERAL">SDL_KEYUP</TT>) or a key press (<TTCLASS="LITERAL">SDL_KEYDOWN</TT>) event. The <TTCLASS="STRUCTFIELD"><I>state</I></TT> is largely redundant, it reports the same information as the <TTCLASS="STRUCTFIELD"><I>type</I></TT> field but uses different values (<TTCLASS="LITERAL">SDL_RELEASED</TT> and <TTCLASS="LITERAL">SDL_PRESSED</TT>). The <TTCLASS="STRUCTFIELD"><I>keysym</I></TT> contains information of the key press or release that this event represents (see above).</P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN324"></A>Reading Keyboard Events</H2><P>Reading keybaord events from the event queue is quite simple (the event queue and using it is described <AHREF="sdlevent.html">here</A>). We read events using <AHREF="sdlpollevent.html"><TTCLASS="FUNCTION">SDL_PollEvent</TT></A> in a <TTCLASS="LITERAL">while()</TT> loop and check for <TTCLASS="LITERAL">SDL_KEYUP</TT> and <TTCLASS="LITERAL">SDL_KEYDOWN</TT> events using a <TTCLASS="LITERAL">switch</TT> statement, like so:</P><DIVCLASS="EXAMPLE"><ANAME="AEN334"></A><P><B>Example 3-10. Reading Keyboard Events</B></P><PRECLASS="PROGRAMLISTING">  SDL_Event event;  .  .  /* Poll for events. SDL_PollEvent() returns 0 when there are no  */  /* more events on the event queue, our while loop will exit when */  /* that occurs.                                                  */  while( SDL_PollEvent( &#38;event ) ){    /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */    switch( event.type ){      case SDL_KEYDOWN:        printf( "Key press detected\n" );        break;      case SDL_KEYUP:        printf( "Key release detected\n" );        break;      default:        break;    }  }  .  .</PRE></DIV><P>This is a very basic example. No information about the key press or release is interpreted. We will explore the other extreme out our first full example below - reporting all available information about a keyboard event.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN338"></A>A More Detailed Look</H2><P>Before we can read events SDL must be initialised with <AHREF="sdlinit.html"><TTCLASS="FUNCTION">SDL_Init</TT></A> and a video mode must be set using <AHREF="sdlsetvideomode.html"><TTCLASS="FUNCTION">SDL_SetVideoMode</TT></A

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -