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