include/SDL_keyboard.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2007 14:52:52 +0000
changeset 2268 4baee598306d
parent 2129 047245361002
child 2303 d87417504c75
permissions -rw-r--r--
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan

After lots of discussion with Christian, this is what we came up with:

> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)

Here is the patch he came up with, and his e-mail about it:

Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan

> Sounds great, go ahead and send me a patch.

Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.

One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.

-Christian
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /**
    24  * \file SDL_keyboard.h
    25  *
    26  * Include file for SDL keyboard event handling
    27  */
    28 
    29 #ifndef _SDL_keyboard_h
    30 #define _SDL_keyboard_h
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_error.h"
    34 #include "SDL_keysym.h"
    35 
    36 #include "begin_code.h"
    37 /* Set up for C function definitions, even when using C++ */
    38 #ifdef __cplusplus
    39 /* *INDENT-OFF* */
    40 extern "C" {
    41 /* *INDENT-ON* */
    42 #endif
    43 
    44 /**
    45  * \struct SDL_keysym
    46  *
    47  * \brief The SDL keysym structure, used in key events.
    48  */
    49 typedef struct SDL_keysym
    50 {
    51     Uint8 scancode;             /**< keyboard specific scancode */
    52     Uint8 padding[3];           /**< alignment padding */
    53     SDLKey sym;                 /**< SDL virtual key code - see ::SDLKey for details */
    54     Uint16 mod;                 /**< current key modifiers */
    55     Uint32 unicode;             /**< OBSOLETE, use SDL_TextInputEvent instead */
    56 } SDL_keysym;
    57 
    58 /* Function prototypes */
    59 
    60 /**
    61  * \fn int SDL_GetNumKeyboards(void)
    62  *
    63  * \brief Get the number of keyboard input devices available.
    64  *
    65  * \sa SDL_SelectKeyboard()
    66  */
    67 extern DECLSPEC int SDLCALL SDL_GetNumKeyboards(void);
    68 
    69 /**
    70  * \fn int SDL_SelectKeyboard(int index)
    71  *
    72  * \brief Set the index of the currently selected keyboard.
    73  *
    74  * \return The index of the previously selected keyboard.
    75  *
    76  * \note You can query the currently selected keyboard by passing an index of -1.
    77  *
    78  * \sa SDL_GetNumKeyboards()
    79  */
    80 extern DECLSPEC int SDLCALL SDL_SelectKeyboard(int index);
    81 
    82 /**
    83  * \fn int SDL_EnableUNICODE(int enable)
    84  *
    85  * \brief Enable/Disable UNICODE translation of keyboard input.
    86  *
    87  * \param enable 1 to enable translation, 0 to disable translation, -1 to query translation
    88  *
    89  * \return The previous state of keyboard translation
    90  *
    91  * \note This translation has some overhead, so translation defaults off.
    92  */
    93 extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable);
    94 
    95 /**
    96  * \fn Uint8 *SDL_GetKeyState(int *numkeys)
    97  *
    98  * \brief Get a snapshot of the current state of the selected keyboard.
    99  *
   100  * \param numkeys if non-NULL, receives the length of the returned array.
   101  *
   102  * \return An array of key states. Indexes into this array are obtained by using the SDLK_INDEX() macro on the \link ::SDLPhysicalKey SDLK_* \endlink syms.
   103  *
   104  * Example:
   105  * 	Uint8 *keystate = SDL_GetKeyState(NULL);
   106  *	if ( keystate[SDLK_INDEX(SDLK_RETURN)] ) ... <RETURN> is pressed.
   107  */
   108 extern DECLSPEC Uint8 *SDLCALL SDL_GetKeyState(int *numkeys);
   109 
   110 /**
   111  * \fn SDLMod SDL_GetModState(void)
   112  *
   113  * \brief Get the current key modifier state for the selected keyboard.
   114  */
   115 extern DECLSPEC SDLMod SDLCALL SDL_GetModState(void);
   116 
   117 /**
   118  * \fn void SDL_SetModState(SDLMod modstate)
   119  *
   120  * \brief Set the current key modifier state for the selected keyboard.
   121  *
   122  * \note This does not change the keyboard state, only the key modifier flags.
   123  */
   124 extern DECLSPEC void SDLCALL SDL_SetModState(SDLMod modstate);
   125 
   126 /**
   127  * \fn SDLKey SDL_GetLayoutKey(SDLKey physicalKey)
   128  * 
   129  * \brief Get the layout key code corresponding to the given physical key code according to the current keyboard layout.
   130  *
   131  * See ::SDLKey for details.
   132  *
   133  * If \a physicalKey is not a physical key code, it is returned unchanged.
   134  *
   135  * \sa SDL_GetKeyName()
   136  */
   137 extern DECLSPEC SDLKey SDLCALL SDL_GetLayoutKey(SDLKey physicalKey);
   138 
   139 /**
   140  * \fn const char *SDL_GetKeyName(SDLKey layoutKey)
   141  * 
   142  * \brief Get a human-readable name for a key.
   143  *
   144  * \param layoutKey An SDL layout key code.
   145  *
   146  * If what you have is a physical key code, e.g. from the \link SDL_keysym::sym key.keysym.sym \endlink field of the SDL_Event structure, convert it to a layout key code using SDL_GetLayoutKey() first. Doing this ensures that the returned name matches what users see on their keyboards. Calling this function directly on a physical key code (that is not also a layout key code) is possible, but is not recommended except for debugging purposes. The name returned in that case is the name of the \link ::SDLPhysicalKey SDLK_* \endlink constant and is not suitable for display to users.
   147  *
   148  * \return A pointer to a UTF-8 string that stays valid at least until the next call to this function. If you need it around any longer, you must copy it. Always non-NULL.
   149  *
   150  * \sa SDLKey
   151  */
   152 extern DECLSPEC const char *SDLCALL SDL_GetKeyName(SDLKey layoutKey);
   153 
   154 
   155 /* Ends C function definitions when using C++ */
   156 #ifdef __cplusplus
   157 /* *INDENT-OFF* */
   158 }
   159 /* *INDENT-ON* */
   160 #endif
   161 #include "close_code.h"
   162 
   163 #endif /* _SDL_keyboard_h */
   164 
   165 /* vi: set ts=4 sw=4 expandtab: */