src/events/SDL_keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1283 f214b6fae45a
child 1327 d12a63a8d95a
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* General keyboard handling code for SDL */
slouken@0
    24
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <ctype.h>
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <string.h>
slouken@0
    29
slouken@0
    30
#include "SDL_error.h"
slouken@0
    31
#include "SDL_events.h"
slouken@0
    32
#include "SDL_timer.h"
slouken@0
    33
#include "SDL_events_c.h"
slouken@0
    34
#include "SDL_sysevents.h"
slouken@0
    35
slouken@0
    36
slouken@0
    37
/* Global keystate information */
slouken@0
    38
static Uint8  SDL_KeyState[SDLK_LAST];
slouken@0
    39
static SDLMod SDL_ModState;
slouken@0
    40
int SDL_TranslateUNICODE = 0;
slouken@0
    41
slouken@0
    42
static char *keynames[SDLK_LAST];	/* Array of keycode names */
slouken@0
    43
slouken@0
    44
/*
slouken@0
    45
 * jk 991215 - added
slouken@0
    46
 */
slouken@0
    47
struct {
slouken@0
    48
	int firsttime;    /* if we check against the delay or repeat value */
slouken@0
    49
	int delay;        /* the delay before we start repeating */
slouken@0
    50
	int interval;     /* the delay between key repeat events */
slouken@0
    51
	Uint32 timestamp; /* the time the first keydown event occurred */
slouken@0
    52
slouken@0
    53
	SDL_Event evt;    /* the event we are supposed to repeat */
slouken@0
    54
} SDL_KeyRepeat;
slouken@0
    55
slouken@0
    56
/* Public functions */
slouken@0
    57
int SDL_KeyboardInit(void)
slouken@0
    58
{
slouken@0
    59
	SDL_VideoDevice *video = current_video;
slouken@0
    60
	SDL_VideoDevice *this  = current_video;
slouken@0
    61
	Uint16 i;
slouken@0
    62
slouken@0
    63
	/* Set default mode of UNICODE translation */
slouken@0
    64
	SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
slouken@0
    65
slouken@0
    66
	/* Initialize the tables */
slouken@0
    67
	SDL_ModState = KMOD_NONE;
slouken@0
    68
	for ( i=0; i<SDL_TABLESIZE(keynames); ++i )
slouken@0
    69
		keynames[i] = NULL;
slouken@0
    70
	for ( i=0; i<SDL_TABLESIZE(SDL_KeyState); ++i )
slouken@0
    71
		SDL_KeyState[i] = SDL_RELEASED;
slouken@0
    72
	video->InitOSKeymap(this);
slouken@0
    73
slouken@0
    74
	SDL_EnableKeyRepeat(0, 0);
slouken@0
    75
slouken@0
    76
	/* Fill in the blanks in keynames */
slouken@0
    77
	keynames[SDLK_BACKSPACE] = "backspace";
slouken@0
    78
	keynames[SDLK_TAB] = "tab";
slouken@0
    79
	keynames[SDLK_CLEAR] = "clear";
slouken@0
    80
	keynames[SDLK_RETURN] = "return";
slouken@0
    81
	keynames[SDLK_PAUSE] = "pause";
slouken@0
    82
	keynames[SDLK_ESCAPE] = "escape";
slouken@0
    83
	keynames[SDLK_SPACE] = "space";
slouken@0
    84
	keynames[SDLK_EXCLAIM]  = "!";
slouken@0
    85
	keynames[SDLK_QUOTEDBL]  = "\"";
slouken@0
    86
	keynames[SDLK_HASH]  = "#";
slouken@0
    87
	keynames[SDLK_DOLLAR]  = "$";
slouken@0
    88
	keynames[SDLK_AMPERSAND]  = "&";
slouken@0
    89
	keynames[SDLK_QUOTE] = "'";
slouken@0
    90
	keynames[SDLK_LEFTPAREN] = "(";
slouken@0
    91
	keynames[SDLK_RIGHTPAREN] = ")";
slouken@0
    92
	keynames[SDLK_ASTERISK] = "*";
slouken@0
    93
	keynames[SDLK_PLUS] = "+";
slouken@0
    94
	keynames[SDLK_COMMA] = ",";
slouken@0
    95
	keynames[SDLK_MINUS] = "-";
slouken@0
    96
	keynames[SDLK_PERIOD] = ".";
slouken@0
    97
	keynames[SDLK_SLASH] = "/";
slouken@0
    98
	keynames[SDLK_0] = "0";
slouken@0
    99
	keynames[SDLK_1] = "1";
slouken@0
   100
	keynames[SDLK_2] = "2";
slouken@0
   101
	keynames[SDLK_3] = "3";
slouken@0
   102
	keynames[SDLK_4] = "4";
slouken@0
   103
	keynames[SDLK_5] = "5";
slouken@0
   104
	keynames[SDLK_6] = "6";
slouken@0
   105
	keynames[SDLK_7] = "7";
slouken@0
   106
	keynames[SDLK_8] = "8";
slouken@0
   107
	keynames[SDLK_9] = "9";
slouken@0
   108
	keynames[SDLK_COLON] = ":";
slouken@0
   109
	keynames[SDLK_SEMICOLON] = ";";
slouken@0
   110
	keynames[SDLK_LESS] = "<";
slouken@0
   111
	keynames[SDLK_EQUALS] = "=";
slouken@0
   112
	keynames[SDLK_GREATER] = ">";
slouken@0
   113
	keynames[SDLK_QUESTION] = "?";
slouken@0
   114
	keynames[SDLK_AT] = "@";
slouken@0
   115
	keynames[SDLK_LEFTBRACKET] = "[";
slouken@0
   116
	keynames[SDLK_BACKSLASH] = "\\";
slouken@0
   117
	keynames[SDLK_RIGHTBRACKET] = "]";
slouken@0
   118
	keynames[SDLK_CARET] = "^";
slouken@0
   119
	keynames[SDLK_UNDERSCORE] = "_";
slouken@0
   120
	keynames[SDLK_BACKQUOTE] = "`";
slouken@0
   121
	keynames[SDLK_a] = "a";
slouken@0
   122
	keynames[SDLK_b] = "b";
slouken@0
   123
	keynames[SDLK_c] = "c";
slouken@0
   124
	keynames[SDLK_d] = "d";
slouken@0
   125
	keynames[SDLK_e] = "e";
slouken@0
   126
	keynames[SDLK_f] = "f";
slouken@0
   127
	keynames[SDLK_g] = "g";
slouken@0
   128
	keynames[SDLK_h] = "h";
slouken@0
   129
	keynames[SDLK_i] = "i";
slouken@0
   130
	keynames[SDLK_j] = "j";
slouken@0
   131
	keynames[SDLK_k] = "k";
slouken@0
   132
	keynames[SDLK_l] = "l";
slouken@0
   133
	keynames[SDLK_m] = "m";
slouken@0
   134
	keynames[SDLK_n] = "n";
slouken@0
   135
	keynames[SDLK_o] = "o";
slouken@0
   136
	keynames[SDLK_p] = "p";
slouken@0
   137
	keynames[SDLK_q] = "q";
slouken@0
   138
	keynames[SDLK_r] = "r";
slouken@0
   139
	keynames[SDLK_s] = "s";
slouken@0
   140
	keynames[SDLK_t] = "t";
slouken@0
   141
	keynames[SDLK_u] = "u";
slouken@0
   142
	keynames[SDLK_v] = "v";
slouken@0
   143
	keynames[SDLK_w] = "w";
slouken@0
   144
	keynames[SDLK_x] = "x";
slouken@0
   145
	keynames[SDLK_y] = "y";
slouken@0
   146
	keynames[SDLK_z] = "z";
slouken@0
   147
	keynames[SDLK_DELETE] = "delete";
slouken@0
   148
slouken@0
   149
	keynames[SDLK_WORLD_0] = "world 0";
slouken@0
   150
	keynames[SDLK_WORLD_1] = "world 1";
slouken@0
   151
	keynames[SDLK_WORLD_2] = "world 2";
slouken@0
   152
	keynames[SDLK_WORLD_3] = "world 3";
slouken@0
   153
	keynames[SDLK_WORLD_4] = "world 4";
slouken@0
   154
	keynames[SDLK_WORLD_5] = "world 5";
slouken@0
   155
	keynames[SDLK_WORLD_6] = "world 6";
slouken@0
   156
	keynames[SDLK_WORLD_7] = "world 7";
slouken@0
   157
	keynames[SDLK_WORLD_8] = "world 8";
slouken@0
   158
	keynames[SDLK_WORLD_9] = "world 9";
slouken@0
   159
	keynames[SDLK_WORLD_10] = "world 10";
slouken@0
   160
	keynames[SDLK_WORLD_11] = "world 11";
slouken@0
   161
	keynames[SDLK_WORLD_12] = "world 12";
slouken@0
   162
	keynames[SDLK_WORLD_13] = "world 13";
slouken@0
   163
	keynames[SDLK_WORLD_14] = "world 14";
slouken@0
   164
	keynames[SDLK_WORLD_15] = "world 15";
slouken@0
   165
	keynames[SDLK_WORLD_16] = "world 16";
slouken@0
   166
	keynames[SDLK_WORLD_17] = "world 17";
slouken@0
   167
	keynames[SDLK_WORLD_18] = "world 18";
slouken@0
   168
	keynames[SDLK_WORLD_19] = "world 19";
slouken@0
   169
	keynames[SDLK_WORLD_20] = "world 20";
slouken@0
   170
	keynames[SDLK_WORLD_21] = "world 21";
slouken@0
   171
	keynames[SDLK_WORLD_22] = "world 22";
slouken@0
   172
	keynames[SDLK_WORLD_23] = "world 23";
slouken@0
   173
	keynames[SDLK_WORLD_24] = "world 24";
slouken@0
   174
	keynames[SDLK_WORLD_25] = "world 25";
slouken@0
   175
	keynames[SDLK_WORLD_26] = "world 26";
slouken@0
   176
	keynames[SDLK_WORLD_27] = "world 27";
slouken@0
   177
	keynames[SDLK_WORLD_28] = "world 28";
slouken@0
   178
	keynames[SDLK_WORLD_29] = "world 29";
slouken@0
   179
	keynames[SDLK_WORLD_30] = "world 30";
slouken@0
   180
	keynames[SDLK_WORLD_31] = "world 31";
slouken@0
   181
	keynames[SDLK_WORLD_32] = "world 32";
slouken@0
   182
	keynames[SDLK_WORLD_33] = "world 33";
slouken@0
   183
	keynames[SDLK_WORLD_34] = "world 34";
slouken@0
   184
	keynames[SDLK_WORLD_35] = "world 35";
slouken@0
   185
	keynames[SDLK_WORLD_36] = "world 36";
slouken@0
   186
	keynames[SDLK_WORLD_37] = "world 37";
slouken@0
   187
	keynames[SDLK_WORLD_38] = "world 38";
slouken@0
   188
	keynames[SDLK_WORLD_39] = "world 39";
slouken@0
   189
	keynames[SDLK_WORLD_40] = "world 40";
slouken@0
   190
	keynames[SDLK_WORLD_41] = "world 41";
slouken@0
   191
	keynames[SDLK_WORLD_42] = "world 42";
slouken@0
   192
	keynames[SDLK_WORLD_43] = "world 43";
slouken@0
   193
	keynames[SDLK_WORLD_44] = "world 44";
slouken@0
   194
	keynames[SDLK_WORLD_45] = "world 45";
slouken@0
   195
	keynames[SDLK_WORLD_46] = "world 46";
slouken@0
   196
	keynames[SDLK_WORLD_47] = "world 47";
slouken@0
   197
	keynames[SDLK_WORLD_48] = "world 48";
slouken@0
   198
	keynames[SDLK_WORLD_49] = "world 49";
slouken@0
   199
	keynames[SDLK_WORLD_50] = "world 50";
slouken@0
   200
	keynames[SDLK_WORLD_51] = "world 51";
slouken@0
   201
	keynames[SDLK_WORLD_52] = "world 52";
slouken@0
   202
	keynames[SDLK_WORLD_53] = "world 53";
slouken@0
   203
	keynames[SDLK_WORLD_54] = "world 54";
slouken@0
   204
	keynames[SDLK_WORLD_55] = "world 55";
slouken@0
   205
	keynames[SDLK_WORLD_56] = "world 56";
slouken@0
   206
	keynames[SDLK_WORLD_57] = "world 57";
slouken@0
   207
	keynames[SDLK_WORLD_58] = "world 58";
slouken@0
   208
	keynames[SDLK_WORLD_59] = "world 59";
slouken@0
   209
	keynames[SDLK_WORLD_60] = "world 60";
slouken@0
   210
	keynames[SDLK_WORLD_61] = "world 61";
slouken@0
   211
	keynames[SDLK_WORLD_62] = "world 62";
slouken@0
   212
	keynames[SDLK_WORLD_63] = "world 63";
slouken@0
   213
	keynames[SDLK_WORLD_64] = "world 64";
slouken@0
   214
	keynames[SDLK_WORLD_65] = "world 65";
slouken@0
   215
	keynames[SDLK_WORLD_66] = "world 66";
slouken@0
   216
	keynames[SDLK_WORLD_67] = "world 67";
slouken@0
   217
	keynames[SDLK_WORLD_68] = "world 68";
slouken@0
   218
	keynames[SDLK_WORLD_69] = "world 69";
slouken@0
   219
	keynames[SDLK_WORLD_70] = "world 70";
slouken@0
   220
	keynames[SDLK_WORLD_71] = "world 71";
slouken@0
   221
	keynames[SDLK_WORLD_72] = "world 72";
slouken@0
   222
	keynames[SDLK_WORLD_73] = "world 73";
slouken@0
   223
	keynames[SDLK_WORLD_74] = "world 74";
slouken@0
   224
	keynames[SDLK_WORLD_75] = "world 75";
slouken@0
   225
	keynames[SDLK_WORLD_76] = "world 76";
slouken@0
   226
	keynames[SDLK_WORLD_77] = "world 77";
slouken@0
   227
	keynames[SDLK_WORLD_78] = "world 78";
slouken@0
   228
	keynames[SDLK_WORLD_79] = "world 79";
slouken@0
   229
	keynames[SDLK_WORLD_80] = "world 80";
slouken@0
   230
	keynames[SDLK_WORLD_81] = "world 81";
slouken@0
   231
	keynames[SDLK_WORLD_82] = "world 82";
slouken@0
   232
	keynames[SDLK_WORLD_83] = "world 83";
slouken@0
   233
	keynames[SDLK_WORLD_84] = "world 84";
slouken@0
   234
	keynames[SDLK_WORLD_85] = "world 85";
slouken@0
   235
	keynames[SDLK_WORLD_86] = "world 86";
slouken@0
   236
	keynames[SDLK_WORLD_87] = "world 87";
slouken@0
   237
	keynames[SDLK_WORLD_88] = "world 88";
slouken@0
   238
	keynames[SDLK_WORLD_89] = "world 89";
slouken@0
   239
	keynames[SDLK_WORLD_90] = "world 90";
slouken@0
   240
	keynames[SDLK_WORLD_91] = "world 91";
slouken@0
   241
	keynames[SDLK_WORLD_92] = "world 92";
slouken@0
   242
	keynames[SDLK_WORLD_93] = "world 93";
slouken@0
   243
	keynames[SDLK_WORLD_94] = "world 94";
slouken@0
   244
	keynames[SDLK_WORLD_95] = "world 95";
slouken@0
   245
slouken@0
   246
	keynames[SDLK_KP0] = "[0]";
slouken@0
   247
	keynames[SDLK_KP1] = "[1]";
slouken@0
   248
	keynames[SDLK_KP2] = "[2]";
slouken@0
   249
	keynames[SDLK_KP3] = "[3]";
slouken@0
   250
	keynames[SDLK_KP4] = "[4]";
slouken@0
   251
	keynames[SDLK_KP5] = "[5]";
slouken@0
   252
	keynames[SDLK_KP6] = "[6]";
slouken@0
   253
	keynames[SDLK_KP7] = "[7]";
slouken@0
   254
	keynames[SDLK_KP8] = "[8]";
slouken@0
   255
	keynames[SDLK_KP9] = "[9]";
slouken@0
   256
	keynames[SDLK_KP_PERIOD] = "[.]";
slouken@0
   257
	keynames[SDLK_KP_DIVIDE] = "[/]";
slouken@0
   258
	keynames[SDLK_KP_MULTIPLY] = "[*]";
slouken@0
   259
	keynames[SDLK_KP_MINUS] = "[-]";
slouken@0
   260
	keynames[SDLK_KP_PLUS] = "[+]";
slouken@0
   261
	keynames[SDLK_KP_ENTER] = "enter";
slouken@0
   262
	keynames[SDLK_KP_EQUALS] = "equals";
slouken@0
   263
slouken@0
   264
	keynames[SDLK_UP] = "up";
slouken@0
   265
	keynames[SDLK_DOWN] = "down";
slouken@0
   266
	keynames[SDLK_RIGHT] = "right";
slouken@0
   267
	keynames[SDLK_LEFT] = "left";
slouken@0
   268
	keynames[SDLK_DOWN] = "down";
slouken@0
   269
	keynames[SDLK_INSERT] = "insert";
slouken@0
   270
	keynames[SDLK_HOME] = "home";
slouken@0
   271
	keynames[SDLK_END] = "end";
slouken@0
   272
	keynames[SDLK_PAGEUP] = "page up";
slouken@0
   273
	keynames[SDLK_PAGEDOWN] = "page down";
slouken@0
   274
slouken@0
   275
	keynames[SDLK_F1] = "f1";
slouken@0
   276
	keynames[SDLK_F2] = "f2";
slouken@0
   277
	keynames[SDLK_F3] = "f3";
slouken@0
   278
	keynames[SDLK_F4] = "f4";
slouken@0
   279
	keynames[SDLK_F5] = "f5";
slouken@0
   280
	keynames[SDLK_F6] = "f6";
slouken@0
   281
	keynames[SDLK_F7] = "f7";
slouken@0
   282
	keynames[SDLK_F8] = "f8";
slouken@0
   283
	keynames[SDLK_F9] = "f9";
slouken@0
   284
	keynames[SDLK_F10] = "f10";
slouken@0
   285
	keynames[SDLK_F11] = "f11";
slouken@0
   286
	keynames[SDLK_F12] = "f12";
slouken@0
   287
	keynames[SDLK_F13] = "f13";
slouken@0
   288
	keynames[SDLK_F14] = "f14";
slouken@0
   289
	keynames[SDLK_F15] = "f15";
slouken@0
   290
slouken@0
   291
	keynames[SDLK_NUMLOCK] = "numlock";
slouken@0
   292
	keynames[SDLK_CAPSLOCK] = "caps lock";
slouken@0
   293
	keynames[SDLK_SCROLLOCK] = "scroll lock";
slouken@0
   294
	keynames[SDLK_RSHIFT] = "right shift";
slouken@0
   295
	keynames[SDLK_LSHIFT] = "left shift";
slouken@0
   296
	keynames[SDLK_RCTRL] = "right ctrl";
slouken@0
   297
	keynames[SDLK_LCTRL] = "left ctrl";
slouken@0
   298
	keynames[SDLK_RALT] = "right alt";
slouken@0
   299
	keynames[SDLK_LALT] = "left alt";
slouken@0
   300
	keynames[SDLK_RMETA] = "right meta";
slouken@0
   301
	keynames[SDLK_LMETA] = "left meta";
slouken@0
   302
	keynames[SDLK_LSUPER] = "left super";	/* "Windows" keys */
slouken@0
   303
	keynames[SDLK_RSUPER] = "right super";	
slouken@0
   304
	keynames[SDLK_MODE] = "alt gr";
slouken@0
   305
	keynames[SDLK_COMPOSE] = "compose";
slouken@0
   306
slouken@0
   307
	keynames[SDLK_HELP] = "help";
slouken@0
   308
	keynames[SDLK_PRINT] = "print screen";
slouken@0
   309
	keynames[SDLK_SYSREQ] = "sys req";
slouken@0
   310
	keynames[SDLK_BREAK] = "break";
slouken@0
   311
	keynames[SDLK_MENU] = "menu";
slouken@0
   312
	keynames[SDLK_POWER] = "power";
slouken@0
   313
	keynames[SDLK_EURO] = "euro";
slouken@184
   314
	keynames[SDLK_UNDO] = "undo";
slouken@0
   315
slouken@0
   316
	/* Done.  Whew. */
slouken@0
   317
	return(0);
slouken@0
   318
}
slouken@1123
   319
void SDL_KeyboardQuit(void)
slouken@1123
   320
{
slouken@1123
   321
}
slouken@0
   322
slouken@0
   323
/* We lost the keyboard, so post key up messages for all pressed keys */
slouken@0
   324
void SDL_ResetKeyboard(void)
slouken@0
   325
{
slouken@0
   326
	SDL_keysym keysym;
slouken@0
   327
	SDLKey key;
slouken@0
   328
slouken@0
   329
	memset(&keysym, 0, (sizeof keysym));
slouken@0
   330
	for ( key=SDLK_FIRST; key<SDLK_LAST; ++key ) {
slouken@0
   331
		if ( SDL_KeyState[key] == SDL_PRESSED ) {
slouken@0
   332
			keysym.sym = key;
slouken@0
   333
			SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
slouken@0
   334
		}
slouken@0
   335
	}
slouken@14
   336
	SDL_KeyRepeat.timestamp = 0;
slouken@0
   337
}
slouken@0
   338
slouken@0
   339
int SDL_EnableUNICODE(int enable)
slouken@0
   340
{
slouken@0
   341
	int old_mode;
slouken@0
   342
slouken@0
   343
	old_mode = SDL_TranslateUNICODE;
slouken@0
   344
	if ( enable >= 0 ) {
slouken@0
   345
		SDL_TranslateUNICODE = enable;
slouken@0
   346
	}
slouken@0
   347
	return(old_mode);
slouken@0
   348
}
slouken@0
   349
slouken@0
   350
Uint8 * SDL_GetKeyState (int *numkeys)
slouken@0
   351
{
slouken@0
   352
	if ( numkeys != (int *)0 )
slouken@0
   353
		*numkeys = SDLK_LAST;
slouken@0
   354
	return(SDL_KeyState);
slouken@0
   355
}
slouken@0
   356
SDLMod SDL_GetModState (void)
slouken@0
   357
{
slouken@0
   358
	return(SDL_ModState);
slouken@0
   359
}
slouken@0
   360
void SDL_SetModState (SDLMod modstate)
slouken@0
   361
{
slouken@0
   362
	SDL_ModState = modstate;
slouken@0
   363
}
slouken@0
   364
slouken@0
   365
char *SDL_GetKeyName(SDLKey key)
slouken@0
   366
{
slouken@0
   367
	char *keyname;
slouken@0
   368
slouken@0
   369
	keyname = NULL;
slouken@0
   370
	if ( key < SDLK_LAST ) {
slouken@0
   371
		keyname = keynames[key];
slouken@0
   372
	}
slouken@0
   373
	if ( keyname == NULL ) {
slouken@0
   374
		keyname = "unknown key";
slouken@0
   375
	}
slouken@0
   376
	return(keyname);
slouken@0
   377
}
slouken@0
   378
slouken@0
   379
/* These are global for SDL_eventloop.c */
slouken@0
   380
int SDL_PrivateKeyboard(Uint8 state, SDL_keysym *keysym)
slouken@0
   381
{
slouken@0
   382
	SDL_Event event;
slouken@0
   383
	int posted, repeatable;
slouken@0
   384
	Uint16 modstate;
slouken@0
   385
slouken@0
   386
	memset(&event, 0, sizeof(event));
slouken@0
   387
slouken@0
   388
#if 0
slouken@0
   389
printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym), 
slouken@0
   390
				state == SDL_PRESSED ? "pressed" : "released");
slouken@0
   391
#endif
slouken@0
   392
	/* Set up the keysym */
slouken@0
   393
	modstate = (Uint16)SDL_ModState;
slouken@0
   394
slouken@0
   395
	repeatable = 0;
slouken@0
   396
slouken@0
   397
	if ( state == SDL_PRESSED ) {
slouken@0
   398
		keysym->mod = (SDLMod)modstate;
slouken@0
   399
		switch (keysym->sym) {
slouken@0
   400
			case SDLK_NUMLOCK:
slouken@0
   401
				modstate ^= KMOD_NUM;
slouken@0
   402
				if ( ! (modstate&KMOD_NUM) )
slouken@0
   403
					state = SDL_RELEASED;
slouken@0
   404
				keysym->mod = (SDLMod)modstate;
slouken@0
   405
				break;
slouken@0
   406
			case SDLK_CAPSLOCK:
slouken@0
   407
				modstate ^= KMOD_CAPS;
slouken@0
   408
				if ( ! (modstate&KMOD_CAPS) )
slouken@0
   409
					state = SDL_RELEASED;
slouken@0
   410
				keysym->mod = (SDLMod)modstate;
slouken@0
   411
				break;
slouken@0
   412
			case SDLK_LCTRL:
slouken@0
   413
				modstate |= KMOD_LCTRL;
slouken@0
   414
				break;
slouken@0
   415
			case SDLK_RCTRL:
slouken@0
   416
				modstate |= KMOD_RCTRL;
slouken@0
   417
				break;
slouken@0
   418
			case SDLK_LSHIFT:
slouken@0
   419
				modstate |= KMOD_LSHIFT;
slouken@0
   420
				break;
slouken@0
   421
			case SDLK_RSHIFT:
slouken@0
   422
				modstate |= KMOD_RSHIFT;
slouken@0
   423
				break;
slouken@0
   424
			case SDLK_LALT:
slouken@0
   425
				modstate |= KMOD_LALT;
slouken@0
   426
				break;
slouken@0
   427
			case SDLK_RALT:
slouken@0
   428
				modstate |= KMOD_RALT;
slouken@0
   429
				break;
slouken@0
   430
			case SDLK_LMETA:
slouken@0
   431
				modstate |= KMOD_LMETA;
slouken@0
   432
				break;
slouken@0
   433
			case SDLK_RMETA:
slouken@0
   434
				modstate |= KMOD_RMETA;
slouken@0
   435
				break;
slouken@0
   436
			case SDLK_MODE:
slouken@0
   437
				modstate |= KMOD_MODE;
slouken@0
   438
				break;
slouken@0
   439
			default:
slouken@0
   440
				repeatable = 1;
slouken@0
   441
				break;
slouken@0
   442
		}
slouken@0
   443
	} else {
slouken@0
   444
		switch (keysym->sym) {
slouken@0
   445
			case SDLK_NUMLOCK:
slouken@0
   446
			case SDLK_CAPSLOCK:
slouken@0
   447
				/* Only send keydown events */
slouken@0
   448
				return(0);
slouken@0
   449
			case SDLK_LCTRL:
slouken@0
   450
				modstate &= ~KMOD_LCTRL;
slouken@0
   451
				break;
slouken@0
   452
			case SDLK_RCTRL:
slouken@0
   453
				modstate &= ~KMOD_RCTRL;
slouken@0
   454
				break;
slouken@0
   455
			case SDLK_LSHIFT:
slouken@0
   456
				modstate &= ~KMOD_LSHIFT;
slouken@0
   457
				break;
slouken@0
   458
			case SDLK_RSHIFT:
slouken@0
   459
				modstate &= ~KMOD_RSHIFT;
slouken@0
   460
				break;
slouken@0
   461
			case SDLK_LALT:
slouken@0
   462
				modstate &= ~KMOD_LALT;
slouken@0
   463
				break;
slouken@0
   464
			case SDLK_RALT:
slouken@0
   465
				modstate &= ~KMOD_RALT;
slouken@0
   466
				break;
slouken@0
   467
			case SDLK_LMETA:
slouken@0
   468
				modstate &= ~KMOD_LMETA;
slouken@0
   469
				break;
slouken@0
   470
			case SDLK_RMETA:
slouken@0
   471
				modstate &= ~KMOD_RMETA;
slouken@0
   472
				break;
slouken@0
   473
			case SDLK_MODE:
slouken@0
   474
				modstate &= ~KMOD_MODE;
slouken@0
   475
				break;
slouken@0
   476
			default:
slouken@0
   477
				break;
slouken@0
   478
		}
slouken@0
   479
		keysym->mod = (SDLMod)modstate;
slouken@0
   480
	}
slouken@0
   481
slouken@0
   482
	/* Figure out what type of event this is */
slouken@0
   483
	switch (state) {
slouken@0
   484
		case SDL_PRESSED:
slouken@0
   485
			event.type = SDL_KEYDOWN;
slouken@0
   486
			break;
slouken@0
   487
		case SDL_RELEASED:
slouken@0
   488
			event.type = SDL_KEYUP;
slouken@0
   489
			/*
slouken@0
   490
			 * jk 991215 - Added
slouken@0
   491
			 */
slouken@1021
   492
			if ( SDL_KeyRepeat.timestamp &&
slouken@1021
   493
			     SDL_KeyRepeat.evt.key.keysym.sym == keysym->sym ) {
slouken@0
   494
				SDL_KeyRepeat.timestamp = 0;
slouken@0
   495
			}
slouken@0
   496
			break;
slouken@0
   497
		default:
slouken@0
   498
			/* Invalid state -- bail */
slouken@0
   499
			return(0);
slouken@0
   500
	}
slouken@0
   501
slouken@0
   502
	/* Drop events that don't change state */
slouken@0
   503
	if ( SDL_KeyState[keysym->sym] == state ) {
slouken@1282
   504
#if 0
slouken@1283
   505
printf("Keyboard event didn't change state - dropped!\n");
slouken@1282
   506
#endif
slouken@0
   507
		return(0);
slouken@0
   508
	}
slouken@0
   509
slouken@0
   510
	/* Update internal keyboard state */
slouken@0
   511
	SDL_ModState = (SDLMod)modstate;
slouken@0
   512
	SDL_KeyState[keysym->sym] = state;
slouken@0
   513
slouken@0
   514
	/* Post the event, if desired */
slouken@0
   515
	posted = 0;
slouken@0
   516
	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
slouken@0
   517
		event.key.state = state;
slouken@0
   518
		event.key.keysym = *keysym;
slouken@223
   519
		/*
slouken@223
   520
		 * jk 991215 - Added
slouken@223
   521
		 */
slouken@223
   522
		if (repeatable && (SDL_KeyRepeat.delay != 0)) {
slouken@223
   523
			SDL_KeyRepeat.evt = event;
slouken@223
   524
			SDL_KeyRepeat.firsttime = 1;
slouken@223
   525
			SDL_KeyRepeat.timestamp=SDL_GetTicks();
slouken@223
   526
		}
slouken@0
   527
		if ( (SDL_EventOK == NULL) || SDL_EventOK(&event) ) {
slouken@0
   528
			posted = 1;
slouken@0
   529
			SDL_PushEvent(&event);
slouken@0
   530
		}
slouken@0
   531
	}
slouken@0
   532
	return(posted);
slouken@0
   533
}
slouken@0
   534
slouken@0
   535
/*
slouken@0
   536
 * jk 991215 - Added
slouken@0
   537
 */
slouken@0
   538
void SDL_CheckKeyRepeat(void)
slouken@0
   539
{
slouken@0
   540
	if ( SDL_KeyRepeat.timestamp ) {
slouken@0
   541
		Uint32 now, interval;
slouken@0
   542
slouken@0
   543
		now = SDL_GetTicks();
slouken@0
   544
		interval = (now - SDL_KeyRepeat.timestamp);
slouken@0
   545
		if ( SDL_KeyRepeat.firsttime ) {
slouken@0
   546
			if ( interval > (Uint32)SDL_KeyRepeat.delay ) {
slouken@0
   547
				SDL_KeyRepeat.timestamp = now;
slouken@0
   548
				SDL_KeyRepeat.firsttime = 0;
slouken@0
   549
			}
slouken@0
   550
		} else {
slouken@0
   551
			if ( interval > (Uint32)SDL_KeyRepeat.interval ) {
slouken@0
   552
				SDL_KeyRepeat.timestamp = now;
slouken@223
   553
				if ( (SDL_EventOK == NULL) || SDL_EventOK(&SDL_KeyRepeat.evt) ) {
slouken@223
   554
					SDL_PushEvent(&SDL_KeyRepeat.evt);
slouken@223
   555
				}
slouken@0
   556
			}
slouken@0
   557
		}
slouken@0
   558
	}
slouken@0
   559
}
slouken@0
   560
slouken@0
   561
int SDL_EnableKeyRepeat(int delay, int interval)
slouken@0
   562
{
slouken@122
   563
	if ( (delay < 0) || (interval < 0) ) {
slouken@122
   564
		SDL_SetError("keyboard repeat value less than zero");
slouken@0
   565
		return(-1);
slouken@0
   566
	}
slouken@0
   567
	SDL_KeyRepeat.firsttime = 0;
slouken@0
   568
	SDL_KeyRepeat.delay = delay;
slouken@122
   569
	SDL_KeyRepeat.interval = interval;
slouken@0
   570
	SDL_KeyRepeat.timestamp = 0;
slouken@0
   571
	return(0);
slouken@0
   572
}
slouken@0
   573