2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Io/Key.cpp $
15 * <insert description of file here>
18 * Revision 1.7 2002/06/09 04:41:21 relnev
19 * added copyright header
21 * Revision 1.6 2002/06/05 04:03:32 relnev
22 * finished cfilesystem.
24 * removed some old code.
26 * fixed mouse save off-by-one.
30 * Revision 1.5 2002/05/31 03:34:02 theoddone33
34 * Revision 1.4 2002/05/30 23:46:29 theoddone33
35 * some minor key changes (not necessarily fixes)
37 * Revision 1.3 2002/05/30 16:50:24 theoddone33
38 * Keyboard partially fixed
40 * Revision 1.2 2002/05/29 23:17:50 theoddone33
41 * Non working text code and fixed keys
43 * Revision 1.1.1.1 2002/05/03 03:28:09 root
47 * 6 10/29/99 6:10p Jefff
48 * squashed the damned y/z german issues once and for all
50 * 5 6/07/99 1:21p Dave
51 * Fixed debug console scrolling problem. Thread related.
53 * 4 6/02/99 6:18p Dave
54 * Fixed TNT lockup problems! Wheeeee!
56 * 3 11/05/98 4:18p Dave
57 * First run nebula support. Beefed up localization a bit. Removed all
58 * conditional compiles for foreign versions. Modified mission file
61 * 2 10/07/98 10:53a Dave
64 * 1 10/07/98 10:49a Dave
66 * 37 6/19/98 3:50p Lawrance
67 * change GERMAN to GR_BUILD
69 * 36 6/17/98 11:05a Lawrance
70 * translate french and german keys
72 * 35 6/12/98 4:49p Hoffoss
73 * Added code to remap scancodes for german and french keyboards.
75 * 34 5/20/98 12:10a Mike
78 * 33 5/19/98 12:19p Mike
81 * 32 5/19/98 12:28a Mike
84 * 31 5/18/98 11:01p Mike
85 * Adding support for cheat system.
87 * 30 5/11/98 12:09a Lawrance
88 * Put in code to turn on/off NumLock key when running under 95
90 * 29 5/01/98 4:23p Lawrance
91 * Remap the scancode for the UK "\" key
93 * 28 4/18/98 12:42p John
94 * Added code to use DirectInput to read keyboard. Took out because it
95 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
97 * 27 4/13/98 10:16a John
98 * Switched gettime back to timer_get_milliseconds, which is now thread
101 * 26 4/12/98 11:08p Lawrance
102 * switch back to using gettime() in separate threads
104 * 25 4/12/98 5:31p Lawrance
105 * use timer_get_milliseconds() instead of gettime()
107 * 24 3/25/98 8:08p John
108 * Restructured software rendering into two modules; One for windowed
109 * debug mode and one for DirectX fullscreen.
111 * 23 1/23/98 3:49p Mike
112 * Fix bug in negative time-down due to latency.
114 * 22 1/07/98 6:41p Lawrance
115 * Pass message latency to the keyboard lib.
117 * 21 11/17/97 10:42a John
118 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
119 * happened, so they're not stuck down.
121 * 20 11/14/97 4:33p Mike
122 * Change Debug key to backquote (from F11).
123 * Balance a ton of subsystems in ships.tbl.
124 * Change "Heavy Laser" to "Disruptor".
126 * 19 9/13/97 9:30a Lawrance
127 * added ability to block certain keys from the keyboard
129 * 18 9/10/97 6:02p Hoffoss
130 * Added code to check for key-pressed sexp operator in FreeSpace as part
131 * of training mission stuff.
133 * 17 9/09/97 11:08a Sandeep
134 * fixed warning level 4
136 * 16 7/29/97 5:30p Lawrance
137 * move gettime() to timer module
139 * 15 4/22/97 10:56a John
140 * fixed some resource leaks.
142 * 14 2/03/97 4:23p Allender
143 * use F11 as debug key now
145 * 13 1/10/97 5:15p Mike
146 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
148 * Added turret code to AI system.
153 //#define USE_DIRECTINPUT
157 #include <windowsx.h>
160 #include <ctype.h> // for toupper
166 #include "localize.h"
168 #define KEY_BUFFER_SIZE 16
170 //-------- Variable accessed by outside functions ---------
171 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
173 uint keyd_last_pressed;
174 uint keyd_last_released;
175 ubyte keyd_pressed[NUM_KEYS];
176 int keyd_time_when_last_pressed;
178 typedef struct keyboard {
179 ushort keybuffer[KEY_BUFFER_SIZE];
180 uint time_pressed[KEY_BUFFER_SIZE];
181 uint TimeKeyWentDown[NUM_KEYS];
182 uint TimeKeyHeldDown[NUM_KEYS];
183 uint TimeKeyDownChecked[NUM_KEYS];
184 uint NumDowns[NUM_KEYS];
185 uint NumUps[NUM_KEYS];
186 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
187 uint keyhead, keytail;
194 CRITICAL_SECTION key_lock;
196 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
197 // This flag was created since the backspace key is also used to correct mistakes
198 // when typing in your pilots callsign. This global flag is checked before execution
202 int SDLtoFS2[SDLK_LAST];
205 int ascii_table[128] =
206 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
207 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
208 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
209 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
210 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
211 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
212 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
213 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
214 255,255,255,255,255,255,255,255 };
216 int shifted_ascii_table[128] =
217 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
218 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
219 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
220 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
221 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
222 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
223 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
224 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
225 255,255,255,255,255,255,255,255 };
227 // used to limit the keypresses that are accepted from the keyboard
228 #define MAX_FILTER_KEYS 64
230 int Key_filter[MAX_FILTER_KEYS];
232 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
233 static int Key_running_NT = 0; // NT is the OS
235 int Cheats_enabled = 0;
236 int Key_normal_game = 0;
241 SDLtoFS2[SDLK_0] = KEY_0;
242 SDLtoFS2[SDLK_1] = KEY_1;
243 SDLtoFS2[SDLK_2] = KEY_2;
244 SDLtoFS2[SDLK_3] = KEY_3;
245 SDLtoFS2[SDLK_4] = KEY_4;
246 SDLtoFS2[SDLK_5] = KEY_5;
247 SDLtoFS2[SDLK_6] = KEY_6;
248 SDLtoFS2[SDLK_7] = KEY_7;
249 SDLtoFS2[SDLK_8] = KEY_8;
250 SDLtoFS2[SDLK_9] = KEY_9;
252 SDLtoFS2[SDLK_a] = KEY_A;
253 SDLtoFS2[SDLK_b] = KEY_B;
254 SDLtoFS2[SDLK_c] = KEY_C;
255 SDLtoFS2[SDLK_d] = KEY_D;
256 SDLtoFS2[SDLK_e] = KEY_E;
257 SDLtoFS2[SDLK_f] = KEY_F;
258 SDLtoFS2[SDLK_g] = KEY_G;
259 SDLtoFS2[SDLK_h] = KEY_H;
260 SDLtoFS2[SDLK_i] = KEY_I;
261 SDLtoFS2[SDLK_j] = KEY_J;
262 SDLtoFS2[SDLK_k] = KEY_K;
263 SDLtoFS2[SDLK_l] = KEY_L;
264 SDLtoFS2[SDLK_m] = KEY_M;
265 SDLtoFS2[SDLK_n] = KEY_N;
266 SDLtoFS2[SDLK_o] = KEY_O;
267 SDLtoFS2[SDLK_p] = KEY_P;
268 SDLtoFS2[SDLK_q] = KEY_Q;
269 SDLtoFS2[SDLK_r] = KEY_R;
270 SDLtoFS2[SDLK_s] = KEY_S;
271 SDLtoFS2[SDLK_t] = KEY_T;
272 SDLtoFS2[SDLK_u] = KEY_U;
273 SDLtoFS2[SDLK_v] = KEY_V;
274 SDLtoFS2[SDLK_w] = KEY_W;
275 SDLtoFS2[SDLK_x] = KEY_X;
276 SDLtoFS2[SDLK_y] = KEY_Y;
277 SDLtoFS2[SDLK_z] = KEY_Z;
279 SDLtoFS2[SDLK_MINUS] = KEY_MINUS;
280 SDLtoFS2[SDLK_EQUALS] = KEY_EQUAL;
281 SDLtoFS2[SDLK_SLASH] = KEY_DIVIDE; // No idea - DDOI
282 SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH;
283 //SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH_UK; // ?
284 SDLtoFS2[SDLK_COMMA] = KEY_COMMA;
285 SDLtoFS2[SDLK_PERIOD] = KEY_PERIOD;
286 SDLtoFS2[SDLK_SEMICOLON] = KEY_SEMICOL;
288 SDLtoFS2[SDLK_LEFTBRACKET] = KEY_LBRACKET;
289 SDLtoFS2[SDLK_RIGHTBRACKET] = KEY_RBRACKET;
291 SDLtoFS2[SDLK_BACKQUOTE] = KEY_RAPOSTRO;
292 SDLtoFS2[SDLK_QUOTE] = KEY_LAPOSTRO;
294 SDLtoFS2[SDLK_ESCAPE] = KEY_ESC;
295 SDLtoFS2[SDLK_RETURN] = KEY_ENTER;
296 SDLtoFS2[SDLK_BACKSPACE] = KEY_BACKSP;
297 SDLtoFS2[SDLK_TAB] = KEY_TAB;
298 SDLtoFS2[SDLK_SPACE] = KEY_SPACEBAR;
300 SDLtoFS2[SDLK_NUMLOCK] = KEY_NUMLOCK;
301 SDLtoFS2[SDLK_SCROLLOCK] = KEY_SCROLLOCK;
302 SDLtoFS2[SDLK_CAPSLOCK] = KEY_CAPSLOCK;
304 SDLtoFS2[SDLK_LSHIFT] = KEY_LSHIFT;
305 SDLtoFS2[SDLK_RSHIFT] = KEY_RSHIFT;
307 SDLtoFS2[SDLK_LALT] = KEY_LALT;
308 SDLtoFS2[SDLK_RALT] = KEY_RALT;
310 SDLtoFS2[SDLK_LCTRL] = KEY_LCTRL;
311 SDLtoFS2[SDLK_RCTRL] = KEY_RCTRL;
313 SDLtoFS2[SDLK_F1] = KEY_F1;
314 SDLtoFS2[SDLK_F2] = KEY_F2;
315 SDLtoFS2[SDLK_F3] = KEY_F3;
316 SDLtoFS2[SDLK_F4] = KEY_F4;
317 SDLtoFS2[SDLK_F5] = KEY_F5;
318 SDLtoFS2[SDLK_F6] = KEY_F6;
319 SDLtoFS2[SDLK_F7] = KEY_F7;
320 SDLtoFS2[SDLK_F8] = KEY_F8;
321 SDLtoFS2[SDLK_F9] = KEY_F9;
322 SDLtoFS2[SDLK_F10] = KEY_F10;
323 SDLtoFS2[SDLK_F11] = KEY_F11;
324 SDLtoFS2[SDLK_F12] = KEY_F12;
326 SDLtoFS2[SDLK_KP0] = KEY_PAD0;
327 SDLtoFS2[SDLK_KP1] = KEY_PAD1;
328 SDLtoFS2[SDLK_KP2] = KEY_PAD2;
329 SDLtoFS2[SDLK_KP3] = KEY_PAD3;
330 SDLtoFS2[SDLK_KP4] = KEY_PAD4;
331 SDLtoFS2[SDLK_KP5] = KEY_PAD5;
332 SDLtoFS2[SDLK_KP6] = KEY_PAD6;
333 SDLtoFS2[SDLK_KP7] = KEY_PAD7;
334 SDLtoFS2[SDLK_KP8] = KEY_PAD8;
335 SDLtoFS2[SDLK_KP9] = KEY_PAD9;
336 SDLtoFS2[SDLK_KP_MINUS] = KEY_PADMINUS;
337 SDLtoFS2[SDLK_KP_PLUS] = KEY_PADPLUS;
338 SDLtoFS2[SDLK_KP_PERIOD] = KEY_PADPERIOD;
339 SDLtoFS2[SDLK_KP_DIVIDE] = KEY_PADDIVIDE;
340 SDLtoFS2[SDLK_KP_MULTIPLY] = KEY_PADMULTIPLY;
341 SDLtoFS2[SDLK_KP_ENTER] = KEY_PADENTER;
343 SDLtoFS2[SDLK_INSERT] = KEY_INSERT;
344 SDLtoFS2[SDLK_HOME] = KEY_HOME;
345 SDLtoFS2[SDLK_PAGEUP] = KEY_PAGEUP;
346 SDLtoFS2[SDLK_DELETE] = KEY_DELETE;
347 SDLtoFS2[SDLK_END] = KEY_END;
348 SDLtoFS2[SDLK_PAGEDOWN] = KEY_PAGEDOWN;
349 SDLtoFS2[SDLK_UP] = KEY_UP;
350 SDLtoFS2[SDLK_DOWN] = KEY_DOWN;
351 SDLtoFS2[SDLK_LEFT] = KEY_LEFT;
352 SDLtoFS2[SDLK_RIGHT] = KEY_RIGHT;
354 SDLtoFS2[SDLK_PRINT] = KEY_PRINT_SCRN;
355 SDLtoFS2[SDLK_PAUSE] = KEY_PAUSE;
356 SDLtoFS2[SDLK_BREAK] = KEY_BREAK;
360 int key_numlock_is_on()
364 SDL_GetKeyState(keys);
365 if ( keys[SDLK_NUMLOCK] ) {
369 unsigned char keys[256];
370 GetKeyboardState(keys);
371 if ( keys[VK_NUMLOCK] ) {
378 void key_turn_off_numlock()
381 // STUB_FUNCTION; /* sdl doesn't support this */
383 unsigned char keys[256];
384 GetKeyboardState(keys);
385 keys[VK_NUMLOCK] = 0;
386 SetKeyboardState(keys);
390 void key_turn_on_numlock()
393 // STUB_FUNCTION; /* sdl doesn't support this */
395 unsigned char keys[256];
396 GetKeyboardState(keys);
397 keys[VK_NUMLOCK] = 1;
398 SetKeyboardState(keys);
402 // Convert a BIOS scancode to ASCII.
403 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
404 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
405 int key_to_ascii(int keycode )
409 if ( !key_inited ) return 255;
411 shifted = keycode & KEY_SHIFTED;
418 return shifted_ascii_table[keycode];
420 return ascii_table[keycode];
423 // Flush the keyboard buffer.
424 // Clear the keyboard array (keyd_pressed).
430 if ( !key_inited ) return;
432 ENTER_CRITICAL_SECTION(&key_lock);
434 key_data.keyhead = key_data.keytail = 0;
436 //Clear the keyboard buffer
437 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
438 key_data.keybuffer[i] = 0;
439 key_data.time_pressed[i] = 0;
442 //Clear the keyboard array
444 CurTime = timer_get_milliseconds();
447 for (i=0; i<NUM_KEYS; i++ ) {
449 key_data.TimeKeyDownChecked[i] = CurTime;
450 key_data.TimeKeyWentDown[i] = CurTime;
451 key_data.TimeKeyHeldDown[i] = 0;
452 key_data.NumDowns[i]=0;
453 key_data.NumUps[i]=0;
456 LEAVE_CRITICAL_SECTION(&key_lock);
459 // A nifty function which performs the function:
460 // n = (n+1) % KEY_BUFFER_SIZE
461 // (assuming positive values of n).
465 if ( n >= KEY_BUFFER_SIZE ) n=0;
469 // Returns 1 if character waiting... 0 otherwise
472 int is_one_waiting = 0;
474 if ( !key_inited ) return 0;
476 ENTER_CRITICAL_SECTION(&key_lock);
478 if (key_data.keytail != key_data.keyhead){
482 LEAVE_CRITICAL_SECTION(&key_lock);
484 return is_one_waiting;
487 // Return key scancode if a key has been pressed,
489 // Reads keys out of the key buffer and updates keyhead.
494 if ( !key_inited ) return 0;
496 ENTER_CRITICAL_SECTION(&key_lock);
498 if (key_data.keytail!=key_data.keyhead) {
499 key = key_data.keybuffer[key_data.keyhead];
500 key_data.keyhead = add_one(key_data.keyhead);
503 LEAVE_CRITICAL_SECTION(&key_lock);
508 // Unget a key. Puts it back in the input queue.
509 void key_outkey(int key)
513 if ( !key_inited ) return;
515 ENTER_CRITICAL_SECTION(&key_lock);
517 bufp = key_data.keytail+1;
519 if (bufp >= KEY_BUFFER_SIZE){
523 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
525 key_data.keytail = bufp;
527 LEAVE_CRITICAL_SECTION(&key_lock);
532 // Return amount of time last key was held down.
533 // This is currently (July 17, 1996) bogus because our timing is
535 int key_inkey_time(uint * time)
544 ENTER_CRITICAL_SECTION(&key_lock);
546 if (key_data.keytail!=key_data.keyhead) {
547 key = key_data.keybuffer[key_data.keyhead];
548 *time = key_data.time_pressed[key_data.keyhead];
549 key_data.keyhead = add_one(key_data.keyhead);
552 LEAVE_CRITICAL_SECTION(&key_lock);
558 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
559 // but does not update keyhead pointer.
564 if ( !key_inited ) return 0;
566 ENTER_CRITICAL_SECTION(&key_lock);
568 if (key_data.keytail!=key_data.keyhead) {
569 key = key_data.keybuffer[key_data.keyhead];
571 LEAVE_CRITICAL_SECTION(&key_lock);
576 // If not installed, uses BIOS and returns getch();
577 // Else returns pending key (or waits for one if none waiting).
583 if ( !key_inited ) return 0;
585 while (!key_checkch()){
595 // Set global shift_status with modifier results (shift, ctrl, alt).
596 uint key_get_shift_status()
598 unsigned int shift_status = 0;
600 if ( !key_inited ) return 0;
602 ENTER_CRITICAL_SECTION(&key_lock);
604 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
605 shift_status |= KEY_SHIFTED;
607 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
608 shift_status |= KEY_ALTED;
610 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
611 shift_status |= KEY_CTRLED;
614 if (keyd_pressed[KEY_DEBUG_KEY])
615 shift_status |= KEY_DEBUGGED;
617 if (keyd_pressed[KEY_DEBUG_KEY]) {
618 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
619 if ((Cheats_enabled) && Key_normal_game) {
620 mprintf(("Debug key\n"));
621 shift_status |= KEY_DEBUGGED1;
625 LEAVE_CRITICAL_SECTION(&key_lock);
630 // Returns amount of time key (specified by "code") has been down since last call.
631 // Returns float, unlike key_down_time() which returns a fix.
632 float key_down_timef(uint scancode)
634 uint time_down, time;
637 if ( !key_inited ) return 0.0f;
639 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
641 ENTER_CRITICAL_SECTION(&key_lock);
643 time = timer_get_milliseconds();
644 delta_time = time - key_data.TimeKeyDownChecked[scancode];
645 key_data.TimeKeyDownChecked[scancode] = time;
647 if ( delta_time <= 1 ) {
648 key_data.TimeKeyWentDown[scancode] = time;
649 if (keyd_pressed[scancode]) {
650 LEAVE_CRITICAL_SECTION(&key_lock);
653 LEAVE_CRITICAL_SECTION(&key_lock);
658 if ( !keyd_pressed[scancode] ) {
659 time_down = key_data.TimeKeyHeldDown[scancode];
660 key_data.TimeKeyHeldDown[scancode] = 0;
662 time_down = time - key_data.TimeKeyWentDown[scancode];
663 key_data.TimeKeyWentDown[scancode] = time;
666 LEAVE_CRITICAL_SECTION(&key_lock);
668 return i2fl(time_down) / i2fl(delta_time);
672 // Returns amount of time key (specified by "code") has been down since last call.
673 // Returns float, unlike key_down_time() which returns a fix.
674 fix key_down_time( uint code )
676 uint time_down, time;
679 if ( !key_inited ) return 0.0f;
681 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
683 EnterCriticalSection( &key_lock );
685 time = timer_get_milliseconds();
686 delta_time = time - TimeKeyDownChecked[scancode];
687 TimeKeyDownChecked[scancode] = time;
689 if ( delta_time <= 1 ) {
690 LeaveCriticalSection( &key_lock );
691 if (keyd_pressed[scancode])
697 if ( !keyd_pressed[scancode] ) {
698 time_down = key_data.TimeKeyHeldDown[scancode];
699 key_data.TimeKeyHeldDown[scancode] = 0;
701 time_down = time - key_data.TimeKeyWentDown[scancode];
702 key_data.TimeKeyWentDown[scancode] = time;
705 LeaveCriticalSection( &key_lock );
707 return fixmuldiv( time_down, F1_0, delta_time );
712 // Returns number of times key has went from up to down since last call.
713 int key_down_count(int scancode)
717 if ( !key_inited ) return 0;
719 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
721 ENTER_CRITICAL_SECTION(&key_lock);
723 n = key_data.NumDowns[scancode];
724 key_data.NumDowns[scancode] = 0;
726 LEAVE_CRITICAL_SECTION(&key_lock);
732 // Returns number of times key has went from down to up since last call.
733 int key_up_count(int scancode)
737 if ( !key_inited ) return 0;
738 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
740 ENTER_CRITICAL_SECTION(&key_lock);
742 n = key_data.NumUps[scancode];
743 key_data.NumUps[scancode] = 0;
745 LEAVE_CRITICAL_SECTION(&key_lock);
750 int key_check(int key)
752 return key_data.down_check[key];
755 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
756 // latency => time difference in ms between when key was actually pressed and now
757 //void key_mark( uint code, int state )
758 void key_mark( uint code, int state, uint latency )
760 uint scancode, breakbit, temp, event_time;
763 if ( !key_inited ) return;
765 ENTER_CRITICAL_SECTION(&key_lock);
767 // If running in the UK, need to translate their wacky slash scancode to ours
768 if ( code == KEY_SLASH_UK ) {
815 if ( (code == 0xc5) && !Key_running_NT ) {
816 key_turn_off_numlock();
819 Assert( code < NUM_KEYS );
821 event_time = timer_get_milliseconds() - latency;
822 // event_time = timeGetTime() - latency;
825 scancode = code & (NUM_KEYS-1);
830 keyd_last_released = scancode;
831 keyd_pressed[scancode] = 0;
832 key_data.NumUps[scancode]++;
834 // What is the point of this code? "temp" is never used!
836 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
837 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
838 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
840 temp |= keyd_pressed[KEY_DEBUG_KEY];
842 if (event_time < key_data.TimeKeyWentDown[scancode])
843 key_data.TimeKeyHeldDown[scancode] = 0;
845 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
848 keyd_last_pressed = scancode;
849 keyd_time_when_last_pressed = event_time;
850 if (!keyd_pressed[scancode]) {
852 key_data.TimeKeyWentDown[scancode] = event_time;
853 keyd_pressed[scancode] = 1;
854 key_data.NumDowns[scancode]++;
855 key_data.down_check[scancode]++;
857 // mprintf(( "Scancode = %x\n", scancode ));
859 // if ( scancode == KEY_BREAK )
863 } else if (!keyd_repeat) {
864 // Don't buffer repeating key if repeat mode is off
868 if ( scancode!=0xAA ) {
869 keycode = (unsigned short)scancode;
871 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
872 keycode |= KEY_SHIFTED;
874 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
875 keycode |= KEY_ALTED;
877 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
878 keycode |= KEY_CTRLED;
881 if ( keyd_pressed[KEY_DEBUG_KEY] )
882 keycode |= KEY_DEBUGGED;
883 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
885 // keyd_pressed[KEY_DEBUG_KEY] = 0;
886 // keyd_pressed[KEY_BACKSP] = 0;
890 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
891 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
892 if (Cheats_enabled && Key_normal_game) {
893 keycode |= KEY_DEBUGGED1;
900 temp = key_data.keytail+1;
901 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
903 if (temp!=key_data.keyhead) {
904 int i, accept_key = 1;
905 // Num_filter_keys will only be non-zero when a key filter has
906 // been explicity set up via key_set_filter()
907 for ( i = 0; i < Num_filter_keys; i++ ) {
909 if ( Key_filter[i] == keycode ) {
916 key_data.keybuffer[key_data.keytail] = keycode;
917 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
918 key_data.keytail = temp;
925 LEAVE_CRITICAL_SECTION(&key_lock);
928 #ifdef USE_DIRECTINPUT
936 if ( !key_inited ) return;
938 #ifdef USE_DIRECTINPUT
942 if ( Key_numlock_was_on ) {
943 key_turn_on_numlock();
944 Key_numlock_was_on = 0;
949 // STUB_FUNCTION; /* don't need this? */
951 DeleteCriticalSection( &key_lock );
958 if ( key_inited ) return;
963 // STUB_FUNCTION; /* don't need this */
965 InitializeCriticalSection( &key_lock );
967 ENTER_CRITICAL_SECTION(&key_lock);
970 keyd_time_when_last_pressed = timer_get_milliseconds();
971 keyd_buffer_type = 1;
974 // Clear the keyboard array
981 // STUB_FUNCTION; /* don't need this */
983 LEAVE_CRITICAL_SECTION(&key_lock);
985 #ifdef USE_DIRECTINPUT
990 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
992 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
996 if ( key_numlock_is_on() ) {
997 Key_numlock_was_on = 1;
998 key_turn_off_numlock();
1006 void key_level_init()
1010 for (i=0; i<NUM_KEYS; i++)
1011 key_data.down_check[i] = 0;
1014 void key_lost_focus()
1016 if ( !key_inited ) return;
1021 void key_got_focus()
1023 if ( !key_inited ) return;
1028 // Restricts the keys that are accepted from the keyboard
1030 // filter_array => array of keys to act as a filter
1031 // num => number of keys in filter_array
1033 void key_set_filter(int *filter_array, int num)
1037 if ( num >= MAX_FILTER_KEYS ) {
1039 num = MAX_FILTER_KEYS;
1042 Num_filter_keys = num;
1044 for ( i = 0; i < num; i++ ) {
1045 Key_filter[i] = filter_array[i];
1049 // Clear the key filter, so all keypresses are accepted from keyboard
1051 void key_clear_filter()
1055 Num_filter_keys = 0;
1056 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
1062 #ifdef USE_DIRECTINPUT
1064 // JAS - April 18, 1998
1065 // Not using because DI has the following problems: (Everything else works ok)
1066 // Under NT, Pause and Numlock report as identical keys.
1067 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
1071 //============================================================================
1072 // Direct Input code
1073 // For the keyboard, this basically replaces our old functionallity of:
1078 //============================================================================
1081 #include "vdinput.h"
1083 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
1085 static LPDIRECTINPUT Di_object = NULL;
1086 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
1087 static HANDLE Di_thread = NULL;
1088 static DWORD Di_thread_id = NULL;
1089 static HANDLE Di_event = NULL;
1091 DWORD di_process(DWORD lparam)
1094 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
1096 //mprintf(( "Got event!\n" ));
1100 DIDEVICEOBJECTDATA rgdod[10];
1101 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
1104 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
1106 if (hr == DIERR_INPUTLOST) {
1108 * DirectInput is telling us that the input stream has
1109 * been interrupted. We aren't tracking any state
1110 * between polls, so we don't have any special reset
1111 * that needs to be done. We just re-acquire and
1114 Sleep(1000); // Pause a second...
1115 hr = Di_keyboard->Acquire();
1116 if (SUCCEEDED(hr)) {
1121 if (SUCCEEDED(hr)) {
1122 // dwItems = number of elements read (could be zero)
1123 if (hr == DI_BUFFEROVERFLOW) {
1124 // Buffer had overflowed.
1125 mprintf(( "Buffer overflowed!\n" ));
1129 //mprintf(( "Got %d events\n", dwItems ));
1131 for (i=0; i<(int)dwItems; i++ ) {
1132 int key = rgdod[i].dwOfs;
1133 int state = rgdod[i].dwData;
1134 int stamp = rgdod[i].dwTimeStamp;
1137 latency = timeGetTime() - stamp;
1141 // if ( key == KEY_PRINT_SCRN ) {
1142 // key_mark( key, 1, latency );
1144 // key_mark( key, (state&0x80?1:0), latency );
1145 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1165 * Register with the DirectInput subsystem and get a pointer
1166 * to a IDirectInput interface we can use.
1172 * Instance handle to our application or DLL.
1174 * DIRECTINPUT_VERSION
1176 * The version of DirectInput we were designed for.
1177 * We take the value from the <dinput.h> header file.
1181 * Receives pointer to the IDirectInput interface
1186 * We do not use OLE aggregation, so this parameter
1190 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1193 mprintf(( "DirectInputCreate failed!\n" ));
1198 * Obtain an interface to the system keyboard device.
1204 * The instance GUID for the device we wish to access.
1205 * GUID_SysKeyboard is a predefined instance GUID that
1206 * always refers to the system keyboard device.
1210 * Receives pointer to the IDirectInputDevice interface
1215 * We do not use OLE aggregation, so this parameter
1219 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1222 mprintf(( "CreateDevice failed!\n" ));
1227 * Set the data format to "keyboard format".
1229 * A data format specifies which controls on a device we
1230 * are interested in, and how they should be reported.
1232 * This tells DirectInput that we will be passing an array
1233 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1239 * Predefined data format which describes
1240 * an array of 256 bytes, one per scancode.
1242 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1245 mprintf(( "SetDataFormat failed!\n" ));
1251 * Set the cooperativity level to let DirectInput know how
1252 * this device should interact with the system and with other
1253 * DirectInput applications.
1257 * DISCL_NONEXCLUSIVE
1259 * Retrieve keyboard data when acquired, not interfering
1260 * with any other applications which are reading keyboard
1265 * If the user switches away from our application,
1266 * automatically release the keyboard back to the system.
1269 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1272 mprintf(( "SetCooperativeLevel failed!\n" ));
1278 // Turn on buffering
1279 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1280 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1282 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1283 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1285 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1287 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1292 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1293 Assert(Di_event != NULL);
1295 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1296 Assert( Di_thread != NULL );
1298 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1300 hr = Di_keyboard->SetEventNotification(Di_event);
1302 mprintf(( "SetEventNotification failed\n" ));
1306 Di_keyboard->Acquire();
1314 * Destroy any lingering IDirectInputDevice object.
1319 * Cleanliness is next to godliness. Unacquire the device
1320 * one last time just in case we got really confused and tried
1321 * to exit while the device is still acquired.
1323 Di_keyboard->Unacquire();
1325 Di_keyboard->Release();
1330 * Destroy any lingering IDirectInput object.
1333 Di_object->Release();
1338 CloseHandle(Di_event);