changed path for SDL headers
[btb/d2x.git] / arch / sdl / key.c
1 /* $Id: key.c,v 1.2 2003-01-15 02:42:41 btb Exp $ */
2 /*
3  *
4  * SDL keyboard input support
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <SDL.h>
17
18 #include "event.h"
19 #include "error.h"
20 #include "key.h"
21 #include "timer.h"
22
23 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
24 #include "d_delay.h"
25 //end this section addition - Matt Mueller
26
27 #define KEY_BUFFER_SIZE 16
28
29 static unsigned char Installed = 0;
30
31 //-------- Variable accessed by outside functions ---------
32 unsigned char           keyd_buffer_type;               // 0=No buffer, 1=buffer ASCII, 2=buffer scans
33 unsigned char           keyd_repeat;
34 unsigned char           keyd_editor_mode;
35 volatile unsigned char  keyd_last_pressed;
36 volatile unsigned char  keyd_last_released;
37 volatile unsigned char  keyd_pressed[256];
38 volatile int            keyd_time_when_last_pressed;
39
40 typedef struct Key_info {
41         ubyte           state;                  // state of key 1 == down, 0 == up
42         ubyte           last_state;             // previous state of key
43         int             counter;                // incremented each time key is down in handler
44         fix             timewentdown;   // simple counter incremented each time in interrupt and key is down
45         fix             timehelddown;   // counter to tell how long key is down -- gets reset to 0 by key routines
46         ubyte           downcount;              // number of key counts key was down
47         ubyte           upcount;                // number of times key was released
48 } Key_info;
49
50 typedef struct keyboard {
51         unsigned short          keybuffer[KEY_BUFFER_SIZE];
52         Key_info                keys[256];
53         fix                     time_pressed[KEY_BUFFER_SIZE];
54         unsigned int            keyhead, keytail;
55 } keyboard;
56
57 static keyboard key_data;
58
59 typedef struct key_props {
60         char *key_text;
61         unsigned char ascii_value;
62         unsigned char shifted_ascii_value;
63         SDLKey sym;
64 } key_props;
65
66 key_props key_properties[256] = {
67 { "",       255,    255,    -1                 },
68 { "ESC",    255,    255,    SDLK_ESCAPE        },
69 { "1",      '1',    '!',    SDLK_1             },
70 { "2",      '2',    '@',    SDLK_2             },
71 { "3",      '3',    '#',    SDLK_3             },
72 { "4",      '4',    '$',    SDLK_4             },
73 { "5",      '5',    '%',    SDLK_5             },
74 { "6",      '6',    '^',    SDLK_6             },
75 { "7",      '7',    '&',    SDLK_7             },
76 { "8",      '8',    '*',    SDLK_8             },
77 { "9",      '9',    '(',    SDLK_9             },
78 { "0",      '0',    ')',    SDLK_0             },
79 { "-",      '-',    '_',    SDLK_MINUS         },
80 { "=",      '=',    '+',    SDLK_EQUALS        },
81 { "BSPC",   255,    255,    SDLK_BACKSPACE     },
82 { "TAB",    255,    255,    SDLK_TAB           },
83 { "Q",      'q',    'Q',    SDLK_q             },
84 { "W",      'w',    'W',    SDLK_w             },
85 { "E",      'e',    'E',    SDLK_e             },
86 { "R",      'r',    'R',    SDLK_r             },
87 { "T",      't',    'T',    SDLK_t             },
88 { "Y",      'y',    'Y',    SDLK_y             },
89 { "U",      'u',    'U',    SDLK_u             },
90 { "I",      'i',    'I',    SDLK_i             },
91 { "O",      'o',    'O',    SDLK_o             },
92 { "P",      'p',    'P',    SDLK_p             },
93 { "[",      '[',    '{',    SDLK_LEFTBRACKET   },
94 { "]",      ']',    '}',    SDLK_RIGHTBRACKET  },
95 //edited 06/08/99 Matt Mueller - set to correct key_text
96 { "\83",      255,    255,    SDLK_RETURN        },
97 //end edit -MM
98 { "LCTRL",  255,    255,    SDLK_LCTRL         },
99 { "A",      'a',    'A',    SDLK_a             },
100 { "S",      's',    'S',    SDLK_s             },
101 { "D",      'd',    'D',    SDLK_d             },
102 { "F",      'f',    'F',    SDLK_f             },
103 { "G",      'g',    'G',    SDLK_g             },
104 { "H",      'h',    'H',    SDLK_h             },
105 { "J",      'j',    'J',    SDLK_j             },
106 { "K",      'k',    'K',    SDLK_k             },
107 { "L",      'l',    'L',    SDLK_l             },
108 //edited 06/08/99 Matt Mueller - set to correct sym
109 { ";",      ';',    ':',    SDLK_SEMICOLON         },
110 //end edit -MM
111 { "'",      '\'',   '"',    SDLK_QUOTE         },
112 //edited 06/08/99 Matt Mueller - set to correct sym
113 { "`",      '`',    '~',    SDLK_BACKQUOTE     },
114 //end edit -MM
115 { "LSHFT",  255,    255,    SDLK_LSHIFT        },
116 { "\\",     '\\',   '|',    SDLK_BACKSLASH     },
117 { "Z",      'z',    'Z',    SDLK_z             },
118 { "X",      'x',    'X',    SDLK_x             },
119 { "C",      'c',    'C',    SDLK_c             },
120 { "V",      'v',    'V',    SDLK_v             },
121 { "B",      'b',    'B',    SDLK_b             },
122 { "N",      'n',    'N',    SDLK_n             },
123 { "M",      'm',    'M',    SDLK_m             },
124 //edited 06/08/99 Matt Mueller - set to correct syms
125 { ",",      ',',    '<',    SDLK_COMMA  },
126 { ".",      '.',    '>',    SDLK_PERIOD },
127 { "/",      '/',    '?',    SDLK_SLASH  },
128 //end edit -MM
129 { "RSHFT",  255,    255,    SDLK_RSHIFT },
130 { "PAD*",   '*',    255,    SDLK_KP_MULTIPLY   },
131 { "LALT",   255,    255,    SDLK_LALT          },
132 { "SPC",    ' ',    ' ',    SDLK_SPACE         },
133 { "CPSLK",  255,    255,    SDLK_CAPSLOCK      },
134 { "F1",     255,    255,    SDLK_F1            },
135 { "F2",     255,    255,    SDLK_F2            },
136 { "F3",     255,    255,    SDLK_F3            },
137 { "F4",     255,    255,    SDLK_F4            },
138 { "F5",     255,    255,    SDLK_F5            },
139 { "F6",     255,    255,    SDLK_F6            },
140 { "F7",     255,    255,    SDLK_F7            },
141 { "F8",     255,    255,    SDLK_F8            },
142 { "F9",     255,    255,    SDLK_F9            },
143 { "F10",    255,    255,    SDLK_F10           },
144 { "NMLCK",  255,    255,    SDLK_NUMLOCK       },
145 { "SCLK",   255,    255,    SDLK_SCROLLOCK     },
146 { "PAD7",   255,    255,    SDLK_KP7           },
147 { "PAD8",   255,    255,    SDLK_KP8           },
148 { "PAD9",   255,    255,    SDLK_KP9           },
149 { "PAD-",   255,    255,    SDLK_KP_MINUS      },
150 { "PAD4",   255,    255,    SDLK_KP4           },
151 { "PAD5",   255,    255,    SDLK_KP5           },
152 { "PAD6",   255,    255,    SDLK_KP6           },
153 { "PAD+",   255,    255,    SDLK_KP_PLUS       },
154 { "PAD1",   255,    255,    SDLK_KP1           },
155 { "PAD2",   255,    255,    SDLK_KP2           },
156 { "PAD3",   255,    255,    SDLK_KP3           },
157 { "PAD0",   255,    255,    SDLK_KP0           },
158 { "PAD.",   255,    255,    SDLK_KP_PERIOD     },
159 { "",       255,    255,    -1                 },
160 { "",       255,    255,    -1                 },
161 { "",       255,    255,    -1                 },
162 { "F11",    255,    255,    SDLK_F11           },
163 { "F12",    255,    255,    SDLK_F12           },
164 { "",       255,    255,    -1                 },       
165 { "",       255,    255,    -1                 },
166 { "",       255,    255,    -1                 },
167 { "",       255,    255,    -1                 },
168 { "",       255,    255,    -1                 },
169 { "",       255,    255,    -1                 },
170 { "",       255,    255,    -1                 },
171 { "",       255,    255,    -1                 },
172 //edited 06/08/99 Matt Mueller - add pause ability
173 { "PAUSE",       255,    255,    SDLK_PAUSE                 },
174 //end edit -MM
175 { "",       255,    255,    -1                 },
176 { "",       255,    255,    -1                 },
177 { "",       255,    255,    -1                 },
178 { "",       255,    255,    -1                 },
179 { "",       255,    255,    -1                 },
180 { "",       255,    255,    -1                 },
181 { "",       255,    255,    -1                 },
182 { "",       255,    255,    -1                 },
183 { "",       255,    255,    -1                 },
184 { "",       255,    255,    -1                 },
185 { "",       255,    255,    -1                 },
186 { "",       255,    255,    -1                 },
187 { "",       255,    255,    -1                 },
188 { "",       255,    255,    -1                 },
189 { "",       255,    255,    -1                 },
190 { "",       255,    255,    -1                 },
191 { "",       255,    255,    -1                 },
192 { "",       255,    255,    -1                 },
193 { "",       255,    255,    -1                 },
194 { "",       255,    255,    -1                 },
195 { "",       255,    255,    -1                 },
196 { "",       255,    255,    -1                 },
197 { "",       255,    255,    -1                 },
198 { "",       255,    255,    -1                 },
199 { "",       255,    255,    -1                 },
200 { "",       255,    255,    -1                 },
201 { "",       255,    255,    -1                 },
202 { "",       255,    255,    -1                 },
203 { "",       255,    255,    -1                 },
204 { "",       255,    255,    -1                 },
205 { "",       255,    255,    -1                 },
206 { "",       255,    255,    -1                 },
207 { "",       255,    255,    -1                 },
208 { "",       255,    255,    -1                 },
209 { "",       255,    255,    -1                 },
210 { "",       255,    255,    -1                 },
211 { "",       255,    255,    -1                 },
212 { "",       255,    255,    -1                 },
213 { "",       255,    255,    -1                 },
214 { "",       255,    255,    -1                 },
215 { "",       255,    255,    -1                 },
216 { "",       255,    255,    -1                 },
217 { "",       255,    255,    -1                 },
218 { "",       255,    255,    -1                 },
219 { "",       255,    255,    -1                 },
220 { "",       255,    255,    -1                 },
221 { "",       255,    255,    -1                 },
222 { "",       255,    255,    -1                 },
223 { "",       255,    255,    -1                 },
224 { "",       255,    255,    -1                 },
225 { "",       255,    255,    -1                 },
226 { "",       255,    255,    -1                 },
227 { "",       255,    255,    -1                 },
228 { "",       255,    255,    -1                 },
229 { "",       255,    255,    -1                 },
230 { "",       255,    255,    -1                 },
231 { "",       255,    255,    -1                 },
232 { "",       255,    255,    -1                 },
233 //edited 06/08/99 Matt Mueller - set to correct key_text
234 { "PAD\83",   255,    255,    SDLK_KP_ENTER      },
235 //end edit -MM
236 //edited 06/08/99 Matt Mueller - set to correct sym
237 { "RCTRL",  255,    255,    SDLK_RCTRL            },
238 //end edit -MM
239 { "",       255,    255,    -1                 },
240 { "",       255,    255,    -1                 },
241 { "",       255,    255,    -1                 },
242 { "",       255,    255,    -1                 },
243 { "",       255,    255,    -1                 },
244 { "",       255,    255,    -1                 },
245 { "",       255,    255,    -1                 },
246 { "",       255,    255,    -1                 },
247 { "",       255,    255,    -1                 },
248 { "",       255,    255,    -1                 },
249 { "",       255,    255,    -1                 },
250 { "",       255,    255,    -1                 },
251 { "",       255,    255,    -1                 },
252 { "",       255,    255,    -1                 },
253 { "",       255,    255,    -1                 },
254 { "",       255,    255,    -1                 },
255 { "",       255,    255,    -1                 },
256 { "",       255,    255,    -1                 },
257 { "",       255,    255,    -1                 },
258 { "",       255,    255,    -1                 },
259 { "",       255,    255,    -1                 },
260 { "",       255,    255,    -1                 },
261 { "",       255,    255,    -1                 },
262 { "PAD/",   255,    255,    SDLK_KP_DIVIDE     },
263 { "",       255,    255,    -1                 },
264 //edited 06/08/99 Matt Mueller - add printscreen ability
265 { "PRSCR",       255,    255,    SDLK_PRINT                 },
266 //end edit -MM
267 { "RALT",   255,    255,    SDLK_RALT          },
268 { "",       255,    255,    -1                 },
269 { "",       255,    255,    -1                 },
270 { "",       255,    255,    -1                 },
271 { "",       255,    255,    -1                 },
272 { "",       255,    255,    -1                 },
273 { "",       255,    255,    -1                 },
274 { "",       255,    255,    -1                 },
275 { "",       255,    255,    -1                 },
276 { "",       255,    255,    -1                 },
277 { "",       255,    255,    -1                 },
278 { "",       255,    255,    -1                 },
279 { "",       255,    255,    -1                 },
280 { "",       255,    255,    -1                 },
281 { "",       255,    255,    -1                 },
282 { "HOME",   255,    255,    SDLK_HOME          },
283 //edited 06/08/99 Matt Mueller - set to correct key_text
284 { "UP",         255,    255,    SDLK_UP            },
285 //end edit -MM
286 { "PGUP",   255,    255,    SDLK_PAGEUP        },
287 { "",       255,    255,    -1                 },
288 //edited 06/08/99 Matt Mueller - set to correct key_text
289 { "LEFT",       255,    255,    SDLK_LEFT          },
290 //end edit -MM
291 { "",       255,    255,    -1                 },
292 //edited 06/08/99 Matt Mueller - set to correct key_text
293 { "RIGHT",      255,    255,    SDLK_RIGHT         },
294 //end edit -MM
295 { "",       255,    255,    -1                 },
296 //edited 06/08/99 Matt Mueller - set to correct key_text
297 { "END",    255,    255,    SDLK_END           },
298 //end edit -MM
299 { "DOWN",       255,    255,    SDLK_DOWN          },
300 { "PGDN",       255,    255,    SDLK_PAGEDOWN      },
301 { "INS",        255,    255,    SDLK_INSERT        },
302 { "DEL",        255,    255,    SDLK_DELETE        },
303 { "",       255,    255,    -1                 },
304 { "",       255,    255,    -1                 },
305 { "",       255,    255,    -1                 },
306 { "",       255,    255,    -1                 },
307 { "",       255,    255,    -1                 },
308 { "",       255,    255,    -1                 },
309 { "",       255,    255,    -1                 },
310 { "",       255,    255,    -1                 },
311 { "",       255,    255,    -1                 },
312 { "",       255,    255,    -1                 },
313 { "",       255,    255,    -1                 },
314 { "",       255,    255,    -1                 },
315 { "",       255,    255,    -1                 },
316 { "",       255,    255,    -1                 },
317 { "",       255,    255,    -1                 },
318 { "",       255,    255,    -1                 },
319 { "",       255,    255,    -1                 },
320 { "",       255,    255,    -1                 },
321 { "",       255,    255,    -1                 },
322 { "",       255,    255,    -1                 },
323 { "",       255,    255,    -1                 },
324 { "",       255,    255,    -1                 },
325 { "",       255,    255,    -1                 },
326 { "",       255,    255,    -1                 },
327 { "",       255,    255,    -1                 },
328 { "",       255,    255,    -1                 },
329 { "",       255,    255,    -1                 },
330 { "",       255,    255,    -1                 },
331 { "",       255,    255,    -1                 },
332 { "",       255,    255,    -1                 },
333 { "",       255,    255,    -1                 },
334 { "",       255,    255,    -1                 },
335 { "",       255,    255,    -1                 },
336 { "",       255,    255,    -1                 },
337 { "",       255,    255,    -1                 },
338 { "",       255,    255,    -1                 },
339 { "",       255,    255,    -1                 },
340 { "",       255,    255,    -1                 },
341 { "",       255,    255,    -1                 },
342 { "",       255,    255,    -1                 },
343 { "",       255,    255,    -1                 },
344 { "",       255,    255,    -1                 },
345 { "",       255,    255,    -1                 },
346 { "",       255,    255,    -1                 },
347 };
348
349 char *key_text[256];
350
351 void key_buid_key_text(void)
352 {
353 }
354
355 unsigned char key_to_ascii(int keycode )
356 {
357         int shifted;
358
359         shifted = keycode & KEY_SHIFTED;
360         keycode &= 0xFF;
361
362         if (shifted)
363                 return key_properties[keycode].shifted_ascii_value;
364         else
365                 return key_properties[keycode].ascii_value;
366 }
367
368 void key_handler(SDL_KeyboardEvent *event)
369 {
370         ubyte state;
371         int i, keycode, event_key, key_state;
372         Key_info *key;
373         unsigned char temp;
374
375         event_key = event->keysym.sym;
376
377         key_state = (event->state == SDL_PRESSED); //  !(wInfo & KF_UP);
378         //=====================================================
379         //Here a translation from win keycodes to mac keycodes!
380         //=====================================================
381
382         for (i = 255; i >= 0; i--) {
383
384                 keycode = i;
385                 key = &(key_data.keys[keycode]);
386                 if (key_properties[i].sym == event_key)
387                         state = key_state;
388                 else
389                         state = key->last_state;
390                         
391                 if ( key->last_state == state ) {
392                         if (state) {
393                                 key->counter++;
394                                 keyd_last_pressed = keycode;
395                                 keyd_time_when_last_pressed = timer_get_fixed_seconds();
396                         }
397                 } else {
398                         if (state)      {
399                                 keyd_last_pressed = keycode;
400                                 keyd_pressed[keycode] = 1;
401                                 key->downcount += state;
402                                 key->state = 1;
403                                 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
404                                 key->counter++;
405                         } else {        
406                                 keyd_pressed[keycode] = 0;
407                                 keyd_last_released = keycode;
408                                 key->upcount += key->state;
409                                 key->state = 0;
410                                 key->counter = 0;
411                                 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
412                         }
413                 }
414                 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
415                         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
416                                 keycode |= KEY_SHIFTED;
417                         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
418                                 keycode |= KEY_ALTED;
419                         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
420                                 keycode |= KEY_CTRLED;
421                         if ( keyd_pressed[KEY_DELETE] )
422                                 keycode |= KEY_DEBUGGED;
423                         temp = key_data.keytail+1;
424                         if ( temp >= KEY_BUFFER_SIZE ) temp=0;
425                         if (temp!=key_data.keyhead)     {
426                                 key_data.keybuffer[key_data.keytail] = keycode;
427                                 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
428                                 key_data.keytail = temp;
429                         }
430                 }
431                 key->last_state = state;
432         }
433 }
434
435 void key_close()
436 {
437  Installed = 0;
438 }
439
440 void key_init()
441 {
442   int i;
443   
444   if (Installed) return;
445
446   Installed=1;
447
448   keyd_time_when_last_pressed = timer_get_fixed_seconds();
449   keyd_buffer_type = 1;
450   keyd_repeat = 1;
451   
452   for(i=0; i<256; i++)
453      key_text[i] = key_properties[i].key_text;
454      
455   // Clear the keyboard array
456   key_flush();
457   atexit(key_close);
458 }
459
460 void key_flush()
461 {
462         int i;
463         fix curtime;
464
465         if (!Installed)
466                 key_init();
467
468         key_data.keyhead = key_data.keytail = 0;
469
470         //Clear the keyboard buffer
471         for (i=0; i<KEY_BUFFER_SIZE; i++ )      {
472                 key_data.keybuffer[i] = 0;
473                 key_data.time_pressed[i] = 0;
474         }
475
476 //use gettimeofday here:
477         curtime = timer_get_fixed_seconds();
478
479         for (i=0; i<256; i++ )  {
480                 keyd_pressed[i] = 0;
481                 key_data.keys[i].state = 1;
482                 key_data.keys[i].last_state = 0;
483                 key_data.keys[i].timewentdown = curtime;
484                 key_data.keys[i].downcount=0;
485                 key_data.keys[i].upcount=0;
486                 key_data.keys[i].timehelddown = 0;
487                 key_data.keys[i].counter = 0;
488         }
489 }
490
491 int add_one(int n)
492 {
493  n++;
494  if ( n >= KEY_BUFFER_SIZE ) n=0;
495  return n;
496 }
497
498 int key_checkch()
499 {
500         int is_one_waiting = 0;
501         event_poll();
502         if (key_data.keytail!=key_data.keyhead)
503                 is_one_waiting = 1;
504         return is_one_waiting;
505 }
506
507 int key_inkey()
508 {
509         int key = 0;
510         if (!Installed)
511                 key_init();
512         event_poll();
513         if (key_data.keytail!=key_data.keyhead) {
514                 key = key_data.keybuffer[key_data.keyhead];
515                 key_data.keyhead = add_one(key_data.keyhead);
516         }
517 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
518         else d_delay(1);
519 //end addition - Matt Mueller
520              
521         return key;
522 }
523
524 int key_inkey_time(fix * time)
525 {
526         int key = 0;
527
528         if (!Installed)
529                 key_init();
530         event_poll();
531         if (key_data.keytail!=key_data.keyhead) {
532                 key = key_data.keybuffer[key_data.keyhead];
533                 *time = key_data.time_pressed[key_data.keyhead];
534                 key_data.keyhead = add_one(key_data.keyhead);
535         }
536         return key;
537 }
538
539 int key_peekkey()
540 {
541         int key = 0;
542         event_poll();
543         if (key_data.keytail!=key_data.keyhead)
544                 key = key_data.keybuffer[key_data.keyhead];
545
546         return key;
547 }
548
549 int key_getch()
550 {
551         int dummy=0;
552
553         if (!Installed)
554                 return 0;
555 //              return getch();
556
557         while (!key_checkch())
558                 dummy++;
559         return key_inkey();
560 }
561
562 unsigned int key_get_shift_status()
563 {
564         unsigned int shift_status = 0;
565
566         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
567                 shift_status |= KEY_SHIFTED;
568
569         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
570                 shift_status |= KEY_ALTED;
571
572         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
573                 shift_status |= KEY_CTRLED;
574
575 #ifndef NDEBUG
576         if (keyd_pressed[KEY_DELETE])
577                 shift_status |=KEY_DEBUGGED;
578 #endif
579
580         return shift_status;
581 }
582
583 // Returns the number of seconds this key has been down since last call.
584 fix key_down_time(int scancode)
585 {
586         fix time_down, time;
587
588         event_poll();
589         if ((scancode<0)|| (scancode>255)) return 0;
590
591         if (!keyd_pressed[scancode]) {
592                 time_down = key_data.keys[scancode].timehelddown;
593                 key_data.keys[scancode].timehelddown = 0;
594         } else {
595                 time = timer_get_fixed_seconds();
596                 time_down = time - key_data.keys[scancode].timewentdown;
597                 key_data.keys[scancode].timewentdown = time;
598         }
599
600         return time_down;
601 }
602
603 unsigned int key_down_count(int scancode)
604 {
605         int n;
606         event_poll();
607         if ((scancode<0)|| (scancode>255)) return 0;
608
609         n = key_data.keys[scancode].downcount;
610         key_data.keys[scancode].downcount = 0;
611
612         return n;
613 }
614
615 unsigned int key_up_count(int scancode)
616 {
617         int n;
618         event_poll();
619         if ((scancode<0)|| (scancode>255)) return 0;
620
621         n = key_data.keys[scancode].upcount;
622         key_data.keys[scancode].upcount = 0;
623
624         return n;
625 }