src/events/SDL_keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 03 Jan 2005 00:24:44 +0000
changeset 1021 ddd058103e28
parent 769 b8d311d90021
child 1123 28ac87a38c17
permissions -rw-r--r--
Date: Sun, 2 Jan 2005 18:36:09 +0100
From: Jon Daniel
Subject: [SDL] KeyRepeat fix

If you switch keys very fast and hold the last one it
doesn't get repeated because the key release of the first key
occured after the key press of the second key.
Unfortunatly the key release of the first key sets
SDL_KeyRepeat.timestamp = 0; causing the the second key not to repeat.

This should be always reproducable. I've only checked this on x86-64
X11.

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