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.10 2003/05/18 03:55:30 taylor
19 * automatic language selection support
21 * Revision 1.9 2003/01/30 19:55:01 relnev
22 * add German keys (this is mostly a patch already sent in by someone else that hasn't made it into cvs yet) (Taylor Richards)
24 * Revision 1.8 2002/06/17 23:11:39 relnev
25 * enable sdl key repeating.
29 * Revision 1.7 2002/06/09 04:41:21 relnev
30 * added copyright header
32 * Revision 1.6 2002/06/05 04:03:32 relnev
33 * finished cfilesystem.
35 * removed some old code.
37 * fixed mouse save off-by-one.
41 * Revision 1.5 2002/05/31 03:34:02 theoddone33
45 * Revision 1.4 2002/05/30 23:46:29 theoddone33
46 * some minor key changes (not necessarily fixes)
48 * Revision 1.3 2002/05/30 16:50:24 theoddone33
49 * Keyboard partially fixed
51 * Revision 1.2 2002/05/29 23:17:50 theoddone33
52 * Non working text code and fixed keys
54 * Revision 1.1.1.1 2002/05/03 03:28:09 root
58 * 6 10/29/99 6:10p Jefff
59 * squashed the damned y/z german issues once and for all
61 * 5 6/07/99 1:21p Dave
62 * Fixed debug console scrolling problem. Thread related.
64 * 4 6/02/99 6:18p Dave
65 * Fixed TNT lockup problems! Wheeeee!
67 * 3 11/05/98 4:18p Dave
68 * First run nebula support. Beefed up localization a bit. Removed all
69 * conditional compiles for foreign versions. Modified mission file
72 * 2 10/07/98 10:53a Dave
75 * 1 10/07/98 10:49a Dave
77 * 37 6/19/98 3:50p Lawrance
78 * change GERMAN to GR_BUILD
80 * 36 6/17/98 11:05a Lawrance
81 * translate french and german keys
83 * 35 6/12/98 4:49p Hoffoss
84 * Added code to remap scancodes for german and french keyboards.
86 * 34 5/20/98 12:10a Mike
89 * 33 5/19/98 12:19p Mike
92 * 32 5/19/98 12:28a Mike
95 * 31 5/18/98 11:01p Mike
96 * Adding support for cheat system.
98 * 30 5/11/98 12:09a Lawrance
99 * Put in code to turn on/off NumLock key when running under 95
101 * 29 5/01/98 4:23p Lawrance
102 * Remap the scancode for the UK "\" key
104 * 28 4/18/98 12:42p John
105 * Added code to use DirectInput to read keyboard. Took out because it
106 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
108 * 27 4/13/98 10:16a John
109 * Switched gettime back to timer_get_milliseconds, which is now thread
112 * 26 4/12/98 11:08p Lawrance
113 * switch back to using gettime() in separate threads
115 * 25 4/12/98 5:31p Lawrance
116 * use timer_get_milliseconds() instead of gettime()
118 * 24 3/25/98 8:08p John
119 * Restructured software rendering into two modules; One for windowed
120 * debug mode and one for DirectX fullscreen.
122 * 23 1/23/98 3:49p Mike
123 * Fix bug in negative time-down due to latency.
125 * 22 1/07/98 6:41p Lawrance
126 * Pass message latency to the keyboard lib.
128 * 21 11/17/97 10:42a John
129 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
130 * happened, so they're not stuck down.
132 * 20 11/14/97 4:33p Mike
133 * Change Debug key to backquote (from F11).
134 * Balance a ton of subsystems in ships.tbl.
135 * Change "Heavy Laser" to "Disruptor".
137 * 19 9/13/97 9:30a Lawrance
138 * added ability to block certain keys from the keyboard
140 * 18 9/10/97 6:02p Hoffoss
141 * Added code to check for key-pressed sexp operator in FreeSpace as part
142 * of training mission stuff.
144 * 17 9/09/97 11:08a Sandeep
145 * fixed warning level 4
147 * 16 7/29/97 5:30p Lawrance
148 * move gettime() to timer module
150 * 15 4/22/97 10:56a John
151 * fixed some resource leaks.
153 * 14 2/03/97 4:23p Allender
154 * use F11 as debug key now
156 * 13 1/10/97 5:15p Mike
157 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
159 * Added turret code to AI system.
164 //#define USE_DIRECTINPUT
168 #include <windowsx.h>
171 #include <ctype.h> // for toupper
177 #include "localize.h"
179 #define KEY_BUFFER_SIZE 16
181 //-------- Variable accessed by outside functions ---------
182 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
184 uint keyd_last_pressed;
185 uint keyd_last_released;
186 ubyte keyd_pressed[NUM_KEYS];
187 int keyd_time_when_last_pressed;
189 typedef struct keyboard {
190 ushort keybuffer[KEY_BUFFER_SIZE];
191 uint time_pressed[KEY_BUFFER_SIZE];
192 uint TimeKeyWentDown[NUM_KEYS];
193 uint TimeKeyHeldDown[NUM_KEYS];
194 uint TimeKeyDownChecked[NUM_KEYS];
195 uint NumDowns[NUM_KEYS];
196 uint NumUps[NUM_KEYS];
197 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
198 uint keyhead, keytail;
205 CRITICAL_SECTION key_lock;
207 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
208 // This flag was created since the backspace key is also used to correct mistakes
209 // when typing in your pilots callsign. This global flag is checked before execution
213 int SDLtoFS2[SDLK_LAST];
216 int ascii_table[128] =
217 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',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', ';', 39, '`',
220 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 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 int shifted_ascii_table[128] =
228 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
229 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
230 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
231 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
232 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
233 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
234 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
235 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
236 255,255,255,255,255,255,255,255 };
238 // used to limit the keypresses that are accepted from the keyboard
239 #define MAX_FILTER_KEYS 64
241 int Key_filter[MAX_FILTER_KEYS];
243 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
244 static int Key_running_NT = 0; // NT is the OS
246 int Cheats_enabled = 0;
247 int Key_normal_game = 0;
252 SDLtoFS2[SDLK_0] = KEY_0;
253 SDLtoFS2[SDLK_1] = KEY_1;
254 SDLtoFS2[SDLK_2] = KEY_2;
255 SDLtoFS2[SDLK_3] = KEY_3;
256 SDLtoFS2[SDLK_4] = KEY_4;
257 SDLtoFS2[SDLK_5] = KEY_5;
258 SDLtoFS2[SDLK_6] = KEY_6;
259 SDLtoFS2[SDLK_7] = KEY_7;
260 SDLtoFS2[SDLK_8] = KEY_8;
261 SDLtoFS2[SDLK_9] = KEY_9;
263 SDLtoFS2[SDLK_a] = KEY_A;
264 SDLtoFS2[SDLK_b] = KEY_B;
265 SDLtoFS2[SDLK_c] = KEY_C;
266 SDLtoFS2[SDLK_d] = KEY_D;
267 SDLtoFS2[SDLK_e] = KEY_E;
268 SDLtoFS2[SDLK_f] = KEY_F;
269 SDLtoFS2[SDLK_g] = KEY_G;
270 SDLtoFS2[SDLK_h] = KEY_H;
271 SDLtoFS2[SDLK_i] = KEY_I;
272 SDLtoFS2[SDLK_j] = KEY_J;
273 SDLtoFS2[SDLK_k] = KEY_K;
274 SDLtoFS2[SDLK_l] = KEY_L;
275 SDLtoFS2[SDLK_m] = KEY_M;
276 SDLtoFS2[SDLK_n] = KEY_N;
277 SDLtoFS2[SDLK_o] = KEY_O;
278 SDLtoFS2[SDLK_p] = KEY_P;
279 SDLtoFS2[SDLK_q] = KEY_Q;
280 SDLtoFS2[SDLK_r] = KEY_R;
281 SDLtoFS2[SDLK_s] = KEY_S;
282 SDLtoFS2[SDLK_t] = KEY_T;
283 SDLtoFS2[SDLK_u] = KEY_U;
284 SDLtoFS2[SDLK_v] = KEY_V;
285 SDLtoFS2[SDLK_w] = KEY_W;
286 SDLtoFS2[SDLK_x] = KEY_X;
287 SDLtoFS2[SDLK_y] = KEY_Y;
288 SDLtoFS2[SDLK_z] = KEY_Z;
291 SDLtoFS2[SDLK_WORLD_63] = KEY_MINUS;
292 SDLtoFS2[SDLK_WORLD_20] = KEY_EQUAL;
293 SDLtoFS2[SDLK_MINUS] = KEY_DIVIDE;
294 SDLtoFS2[SDLK_HASH] = KEY_SLASH;
295 SDLtoFS2[SDLK_COMMA] = KEY_COMMA;
296 SDLtoFS2[SDLK_PERIOD] = KEY_PERIOD;
297 SDLtoFS2[SDLK_WORLD_86] = KEY_SEMICOL;
299 SDLtoFS2[SDLK_WORLD_92] = KEY_LBRACKET;
300 SDLtoFS2[SDLK_PLUS] = KEY_RBRACKET;
302 SDLtoFS2[SDLK_CARET] = KEY_LAPOSTRO;
303 SDLtoFS2[SDLK_WORLD_68] = KEY_RAPOSTRO;
305 SDLtoFS2[SDLK_MINUS] = KEY_MINUS;
306 SDLtoFS2[SDLK_EQUALS] = KEY_EQUAL;
307 SDLtoFS2[SDLK_SLASH] = KEY_DIVIDE; // No idea - DDOI
308 SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH;
309 //SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH_UK; // ?
310 SDLtoFS2[SDLK_COMMA] = KEY_COMMA;
311 SDLtoFS2[SDLK_PERIOD] = KEY_PERIOD;
312 SDLtoFS2[SDLK_SEMICOLON] = KEY_SEMICOL;
314 SDLtoFS2[SDLK_LEFTBRACKET] = KEY_LBRACKET;
315 SDLtoFS2[SDLK_RIGHTBRACKET] = KEY_RBRACKET;
317 SDLtoFS2[SDLK_BACKQUOTE] = KEY_LAPOSTRO;
318 SDLtoFS2[SDLK_QUOTE] = KEY_RAPOSTRO;
321 SDLtoFS2[SDLK_ESCAPE] = KEY_ESC;
322 SDLtoFS2[SDLK_RETURN] = KEY_ENTER;
323 SDLtoFS2[SDLK_BACKSPACE] = KEY_BACKSP;
324 SDLtoFS2[SDLK_TAB] = KEY_TAB;
325 SDLtoFS2[SDLK_SPACE] = KEY_SPACEBAR;
327 SDLtoFS2[SDLK_NUMLOCK] = KEY_NUMLOCK;
328 SDLtoFS2[SDLK_SCROLLOCK] = KEY_SCROLLOCK;
329 SDLtoFS2[SDLK_CAPSLOCK] = KEY_CAPSLOCK;
331 SDLtoFS2[SDLK_LSHIFT] = KEY_LSHIFT;
332 SDLtoFS2[SDLK_RSHIFT] = KEY_RSHIFT;
334 SDLtoFS2[SDLK_LALT] = KEY_LALT;
335 SDLtoFS2[SDLK_RALT] = KEY_RALT;
337 SDLtoFS2[SDLK_LCTRL] = KEY_LCTRL;
338 SDLtoFS2[SDLK_RCTRL] = KEY_RCTRL;
340 SDLtoFS2[SDLK_F1] = KEY_F1;
341 SDLtoFS2[SDLK_F2] = KEY_F2;
342 SDLtoFS2[SDLK_F3] = KEY_F3;
343 SDLtoFS2[SDLK_F4] = KEY_F4;
344 SDLtoFS2[SDLK_F5] = KEY_F5;
345 SDLtoFS2[SDLK_F6] = KEY_F6;
346 SDLtoFS2[SDLK_F7] = KEY_F7;
347 SDLtoFS2[SDLK_F8] = KEY_F8;
348 SDLtoFS2[SDLK_F9] = KEY_F9;
349 SDLtoFS2[SDLK_F10] = KEY_F10;
350 SDLtoFS2[SDLK_F11] = KEY_F11;
351 SDLtoFS2[SDLK_F12] = KEY_F12;
353 SDLtoFS2[SDLK_KP0] = KEY_PAD0;
354 SDLtoFS2[SDLK_KP1] = KEY_PAD1;
355 SDLtoFS2[SDLK_KP2] = KEY_PAD2;
356 SDLtoFS2[SDLK_KP3] = KEY_PAD3;
357 SDLtoFS2[SDLK_KP4] = KEY_PAD4;
358 SDLtoFS2[SDLK_KP5] = KEY_PAD5;
359 SDLtoFS2[SDLK_KP6] = KEY_PAD6;
360 SDLtoFS2[SDLK_KP7] = KEY_PAD7;
361 SDLtoFS2[SDLK_KP8] = KEY_PAD8;
362 SDLtoFS2[SDLK_KP9] = KEY_PAD9;
363 SDLtoFS2[SDLK_KP_MINUS] = KEY_PADMINUS;
364 SDLtoFS2[SDLK_KP_PLUS] = KEY_PADPLUS;
365 SDLtoFS2[SDLK_KP_PERIOD] = KEY_PADPERIOD;
366 SDLtoFS2[SDLK_KP_DIVIDE] = KEY_PADDIVIDE;
367 SDLtoFS2[SDLK_KP_MULTIPLY] = KEY_PADMULTIPLY;
368 SDLtoFS2[SDLK_KP_ENTER] = KEY_PADENTER;
370 SDLtoFS2[SDLK_INSERT] = KEY_INSERT;
371 SDLtoFS2[SDLK_HOME] = KEY_HOME;
372 SDLtoFS2[SDLK_PAGEUP] = KEY_PAGEUP;
373 SDLtoFS2[SDLK_DELETE] = KEY_DELETE;
374 SDLtoFS2[SDLK_END] = KEY_END;
375 SDLtoFS2[SDLK_PAGEDOWN] = KEY_PAGEDOWN;
376 SDLtoFS2[SDLK_UP] = KEY_UP;
377 SDLtoFS2[SDLK_DOWN] = KEY_DOWN;
378 SDLtoFS2[SDLK_LEFT] = KEY_LEFT;
379 SDLtoFS2[SDLK_RIGHT] = KEY_RIGHT;
381 SDLtoFS2[SDLK_PRINT] = KEY_PRINT_SCRN;
382 SDLtoFS2[SDLK_PAUSE] = KEY_PAUSE;
383 SDLtoFS2[SDLK_BREAK] = KEY_BREAK;
387 int key_numlock_is_on()
391 SDL_GetKeyState(keys);
392 if ( keys[SDLK_NUMLOCK] ) {
396 unsigned char keys[256];
397 GetKeyboardState(keys);
398 if ( keys[VK_NUMLOCK] ) {
405 void key_turn_off_numlock()
408 // STUB_FUNCTION; /* sdl doesn't support this */
410 unsigned char keys[256];
411 GetKeyboardState(keys);
412 keys[VK_NUMLOCK] = 0;
413 SetKeyboardState(keys);
417 void key_turn_on_numlock()
420 // STUB_FUNCTION; /* sdl doesn't support this */
422 unsigned char keys[256];
423 GetKeyboardState(keys);
424 keys[VK_NUMLOCK] = 1;
425 SetKeyboardState(keys);
429 // Convert a BIOS scancode to ASCII.
430 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
431 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
432 int key_to_ascii(int keycode )
436 if ( !key_inited ) return 255;
438 shifted = keycode & KEY_SHIFTED;
445 return shifted_ascii_table[keycode];
447 return ascii_table[keycode];
450 // Flush the keyboard buffer.
451 // Clear the keyboard array (keyd_pressed).
457 if ( !key_inited ) return;
459 ENTER_CRITICAL_SECTION(&key_lock);
461 key_data.keyhead = key_data.keytail = 0;
463 //Clear the keyboard buffer
464 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
465 key_data.keybuffer[i] = 0;
466 key_data.time_pressed[i] = 0;
469 //Clear the keyboard array
471 CurTime = timer_get_milliseconds();
474 for (i=0; i<NUM_KEYS; i++ ) {
476 key_data.TimeKeyDownChecked[i] = CurTime;
477 key_data.TimeKeyWentDown[i] = CurTime;
478 key_data.TimeKeyHeldDown[i] = 0;
479 key_data.NumDowns[i]=0;
480 key_data.NumUps[i]=0;
483 LEAVE_CRITICAL_SECTION(&key_lock);
486 // A nifty function which performs the function:
487 // n = (n+1) % KEY_BUFFER_SIZE
488 // (assuming positive values of n).
492 if ( n >= KEY_BUFFER_SIZE ) n=0;
496 // Returns 1 if character waiting... 0 otherwise
499 int is_one_waiting = 0;
501 if ( !key_inited ) return 0;
503 ENTER_CRITICAL_SECTION(&key_lock);
505 if (key_data.keytail != key_data.keyhead){
509 LEAVE_CRITICAL_SECTION(&key_lock);
511 return is_one_waiting;
514 // Return key scancode if a key has been pressed,
516 // Reads keys out of the key buffer and updates keyhead.
521 if ( !key_inited ) return 0;
523 ENTER_CRITICAL_SECTION(&key_lock);
525 if (key_data.keytail!=key_data.keyhead) {
526 key = key_data.keybuffer[key_data.keyhead];
527 key_data.keyhead = add_one(key_data.keyhead);
530 LEAVE_CRITICAL_SECTION(&key_lock);
535 // Unget a key. Puts it back in the input queue.
536 void key_outkey(int key)
540 if ( !key_inited ) return;
542 ENTER_CRITICAL_SECTION(&key_lock);
544 bufp = key_data.keytail+1;
546 if (bufp >= KEY_BUFFER_SIZE){
550 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
552 key_data.keytail = bufp;
554 LEAVE_CRITICAL_SECTION(&key_lock);
559 // Return amount of time last key was held down.
560 // This is currently (July 17, 1996) bogus because our timing is
562 int key_inkey_time(uint * time)
571 ENTER_CRITICAL_SECTION(&key_lock);
573 if (key_data.keytail!=key_data.keyhead) {
574 key = key_data.keybuffer[key_data.keyhead];
575 *time = key_data.time_pressed[key_data.keyhead];
576 key_data.keyhead = add_one(key_data.keyhead);
579 LEAVE_CRITICAL_SECTION(&key_lock);
585 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
586 // but does not update keyhead pointer.
591 if ( !key_inited ) return 0;
593 ENTER_CRITICAL_SECTION(&key_lock);
595 if (key_data.keytail!=key_data.keyhead) {
596 key = key_data.keybuffer[key_data.keyhead];
598 LEAVE_CRITICAL_SECTION(&key_lock);
603 // If not installed, uses BIOS and returns getch();
604 // Else returns pending key (or waits for one if none waiting).
610 if ( !key_inited ) return 0;
612 while (!key_checkch()){
622 // Set global shift_status with modifier results (shift, ctrl, alt).
623 uint key_get_shift_status()
625 unsigned int shift_status = 0;
627 if ( !key_inited ) return 0;
629 ENTER_CRITICAL_SECTION(&key_lock);
631 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
632 shift_status |= KEY_SHIFTED;
634 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
635 shift_status |= KEY_ALTED;
637 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
638 shift_status |= KEY_CTRLED;
641 if (keyd_pressed[KEY_DEBUG_KEY])
642 shift_status |= KEY_DEBUGGED;
644 if (keyd_pressed[KEY_DEBUG_KEY]) {
645 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
646 if ((Cheats_enabled) && Key_normal_game) {
647 mprintf(("Debug key\n"));
648 shift_status |= KEY_DEBUGGED1;
652 LEAVE_CRITICAL_SECTION(&key_lock);
657 // Returns amount of time key (specified by "code") has been down since last call.
658 // Returns float, unlike key_down_time() which returns a fix.
659 float key_down_timef(uint scancode)
661 uint time_down, time;
664 if ( !key_inited ) return 0.0f;
666 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
668 ENTER_CRITICAL_SECTION(&key_lock);
670 time = timer_get_milliseconds();
671 delta_time = time - key_data.TimeKeyDownChecked[scancode];
672 key_data.TimeKeyDownChecked[scancode] = time;
674 if ( delta_time <= 1 ) {
675 key_data.TimeKeyWentDown[scancode] = time;
676 if (keyd_pressed[scancode]) {
677 LEAVE_CRITICAL_SECTION(&key_lock);
680 LEAVE_CRITICAL_SECTION(&key_lock);
685 if ( !keyd_pressed[scancode] ) {
686 time_down = key_data.TimeKeyHeldDown[scancode];
687 key_data.TimeKeyHeldDown[scancode] = 0;
689 time_down = time - key_data.TimeKeyWentDown[scancode];
690 key_data.TimeKeyWentDown[scancode] = time;
693 LEAVE_CRITICAL_SECTION(&key_lock);
695 return i2fl(time_down) / i2fl(delta_time);
699 // Returns amount of time key (specified by "code") has been down since last call.
700 // Returns float, unlike key_down_time() which returns a fix.
701 fix key_down_time( uint code )
703 uint time_down, time;
706 if ( !key_inited ) return 0.0f;
708 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
710 EnterCriticalSection( &key_lock );
712 time = timer_get_milliseconds();
713 delta_time = time - TimeKeyDownChecked[scancode];
714 TimeKeyDownChecked[scancode] = time;
716 if ( delta_time <= 1 ) {
717 LeaveCriticalSection( &key_lock );
718 if (keyd_pressed[scancode])
724 if ( !keyd_pressed[scancode] ) {
725 time_down = key_data.TimeKeyHeldDown[scancode];
726 key_data.TimeKeyHeldDown[scancode] = 0;
728 time_down = time - key_data.TimeKeyWentDown[scancode];
729 key_data.TimeKeyWentDown[scancode] = time;
732 LeaveCriticalSection( &key_lock );
734 return fixmuldiv( time_down, F1_0, delta_time );
739 // Returns number of times key has went from up to down since last call.
740 int key_down_count(int scancode)
744 if ( !key_inited ) return 0;
746 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
748 ENTER_CRITICAL_SECTION(&key_lock);
750 n = key_data.NumDowns[scancode];
751 key_data.NumDowns[scancode] = 0;
753 LEAVE_CRITICAL_SECTION(&key_lock);
759 // Returns number of times key has went from down to up since last call.
760 int key_up_count(int scancode)
764 if ( !key_inited ) return 0;
765 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
767 ENTER_CRITICAL_SECTION(&key_lock);
769 n = key_data.NumUps[scancode];
770 key_data.NumUps[scancode] = 0;
772 LEAVE_CRITICAL_SECTION(&key_lock);
777 int key_check(int key)
779 return key_data.down_check[key];
782 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
783 // latency => time difference in ms between when key was actually pressed and now
784 //void key_mark( uint code, int state )
785 void key_mark( uint code, int state, uint latency )
787 uint scancode, breakbit, temp, event_time;
790 if ( !key_inited ) return;
792 ENTER_CRITICAL_SECTION(&key_lock);
794 // If running in the UK, need to translate their wacky slash scancode to ours
795 if ( code == KEY_SLASH_UK ) {
841 if ( (code == 0xc5) && !Key_running_NT ) {
842 key_turn_off_numlock();
845 Assert( code < NUM_KEYS );
847 event_time = timer_get_milliseconds() - latency;
848 // event_time = timeGetTime() - latency;
851 scancode = code & (NUM_KEYS-1);
856 keyd_last_released = scancode;
857 keyd_pressed[scancode] = 0;
858 key_data.NumUps[scancode]++;
860 // What is the point of this code? "temp" is never used!
862 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
863 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
864 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
866 temp |= keyd_pressed[KEY_DEBUG_KEY];
868 if (event_time < key_data.TimeKeyWentDown[scancode])
869 key_data.TimeKeyHeldDown[scancode] = 0;
871 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
874 keyd_last_pressed = scancode;
875 keyd_time_when_last_pressed = event_time;
876 if (!keyd_pressed[scancode]) {
878 key_data.TimeKeyWentDown[scancode] = event_time;
879 keyd_pressed[scancode] = 1;
880 key_data.NumDowns[scancode]++;
881 key_data.down_check[scancode]++;
883 // mprintf(( "Scancode = %x\n", scancode ));
885 // if ( scancode == KEY_BREAK )
889 } else if (!keyd_repeat) {
890 // Don't buffer repeating key if repeat mode is off
894 if ( scancode!=0xAA ) {
895 keycode = (unsigned short)scancode;
897 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
898 keycode |= KEY_SHIFTED;
900 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
901 keycode |= KEY_ALTED;
903 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
904 keycode |= KEY_CTRLED;
907 if ( keyd_pressed[KEY_DEBUG_KEY] )
908 keycode |= KEY_DEBUGGED;
909 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
911 // keyd_pressed[KEY_DEBUG_KEY] = 0;
912 // keyd_pressed[KEY_BACKSP] = 0;
916 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
917 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
918 if (Cheats_enabled && Key_normal_game) {
919 keycode |= KEY_DEBUGGED1;
926 temp = key_data.keytail+1;
927 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
929 if (temp!=key_data.keyhead) {
930 int i, accept_key = 1;
931 // Num_filter_keys will only be non-zero when a key filter has
932 // been explicity set up via key_set_filter()
933 for ( i = 0; i < Num_filter_keys; i++ ) {
935 if ( Key_filter[i] == keycode ) {
942 key_data.keybuffer[key_data.keytail] = keycode;
943 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
944 key_data.keytail = temp;
951 LEAVE_CRITICAL_SECTION(&key_lock);
954 #ifdef USE_DIRECTINPUT
962 if ( !key_inited ) return;
964 #ifdef USE_DIRECTINPUT
968 if ( Key_numlock_was_on ) {
969 key_turn_on_numlock();
970 Key_numlock_was_on = 0;
975 // STUB_FUNCTION; /* don't need this? */
977 DeleteCriticalSection( &key_lock );
984 if ( key_inited ) return;
989 // STUB_FUNCTION; /* don't need this */
991 InitializeCriticalSection( &key_lock );
993 ENTER_CRITICAL_SECTION(&key_lock);
996 keyd_time_when_last_pressed = timer_get_milliseconds();
997 keyd_buffer_type = 1;
1000 // Clear the keyboard array
1007 // STUB_FUNCTION; /* don't need this */
1009 LEAVE_CRITICAL_SECTION(&key_lock);
1011 #ifdef USE_DIRECTINPUT
1016 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1018 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
1022 if ( key_numlock_is_on() ) {
1023 Key_numlock_was_on = 1;
1024 key_turn_off_numlock();
1032 void key_level_init()
1036 for (i=0; i<NUM_KEYS; i++)
1037 key_data.down_check[i] = 0;
1040 void key_lost_focus()
1042 if ( !key_inited ) return;
1047 void key_got_focus()
1049 if ( !key_inited ) return;
1054 // Restricts the keys that are accepted from the keyboard
1056 // filter_array => array of keys to act as a filter
1057 // num => number of keys in filter_array
1059 void key_set_filter(int *filter_array, int num)
1063 if ( num >= MAX_FILTER_KEYS ) {
1065 num = MAX_FILTER_KEYS;
1068 Num_filter_keys = num;
1070 for ( i = 0; i < num; i++ ) {
1071 Key_filter[i] = filter_array[i];
1075 // Clear the key filter, so all keypresses are accepted from keyboard
1077 void key_clear_filter()
1081 Num_filter_keys = 0;
1082 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
1088 #ifdef USE_DIRECTINPUT
1090 // JAS - April 18, 1998
1091 // Not using because DI has the following problems: (Everything else works ok)
1092 // Under NT, Pause and Numlock report as identical keys.
1093 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
1097 //============================================================================
1098 // Direct Input code
1099 // For the keyboard, this basically replaces our old functionallity of:
1104 //============================================================================
1107 #include "vdinput.h"
1109 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
1111 static LPDIRECTINPUT Di_object = NULL;
1112 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
1113 static HANDLE Di_thread = NULL;
1114 static DWORD Di_thread_id = NULL;
1115 static HANDLE Di_event = NULL;
1117 DWORD di_process(DWORD lparam)
1120 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
1122 //mprintf(( "Got event!\n" ));
1126 DIDEVICEOBJECTDATA rgdod[10];
1127 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
1130 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
1132 if (hr == DIERR_INPUTLOST) {
1134 * DirectInput is telling us that the input stream has
1135 * been interrupted. We aren't tracking any state
1136 * between polls, so we don't have any special reset
1137 * that needs to be done. We just re-acquire and
1140 Sleep(1000); // Pause a second...
1141 hr = Di_keyboard->Acquire();
1142 if (SUCCEEDED(hr)) {
1147 if (SUCCEEDED(hr)) {
1148 // dwItems = number of elements read (could be zero)
1149 if (hr == DI_BUFFEROVERFLOW) {
1150 // Buffer had overflowed.
1151 mprintf(( "Buffer overflowed!\n" ));
1155 //mprintf(( "Got %d events\n", dwItems ));
1157 for (i=0; i<(int)dwItems; i++ ) {
1158 int key = rgdod[i].dwOfs;
1159 int state = rgdod[i].dwData;
1160 int stamp = rgdod[i].dwTimeStamp;
1163 latency = timeGetTime() - stamp;
1167 // if ( key == KEY_PRINT_SCRN ) {
1168 // key_mark( key, 1, latency );
1170 // key_mark( key, (state&0x80?1:0), latency );
1171 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1191 * Register with the DirectInput subsystem and get a pointer
1192 * to a IDirectInput interface we can use.
1198 * Instance handle to our application or DLL.
1200 * DIRECTINPUT_VERSION
1202 * The version of DirectInput we were designed for.
1203 * We take the value from the <dinput.h> header file.
1207 * Receives pointer to the IDirectInput interface
1212 * We do not use OLE aggregation, so this parameter
1216 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1219 mprintf(( "DirectInputCreate failed!\n" ));
1224 * Obtain an interface to the system keyboard device.
1230 * The instance GUID for the device we wish to access.
1231 * GUID_SysKeyboard is a predefined instance GUID that
1232 * always refers to the system keyboard device.
1236 * Receives pointer to the IDirectInputDevice interface
1241 * We do not use OLE aggregation, so this parameter
1245 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1248 mprintf(( "CreateDevice failed!\n" ));
1253 * Set the data format to "keyboard format".
1255 * A data format specifies which controls on a device we
1256 * are interested in, and how they should be reported.
1258 * This tells DirectInput that we will be passing an array
1259 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1265 * Predefined data format which describes
1266 * an array of 256 bytes, one per scancode.
1268 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1271 mprintf(( "SetDataFormat failed!\n" ));
1277 * Set the cooperativity level to let DirectInput know how
1278 * this device should interact with the system and with other
1279 * DirectInput applications.
1283 * DISCL_NONEXCLUSIVE
1285 * Retrieve keyboard data when acquired, not interfering
1286 * with any other applications which are reading keyboard
1291 * If the user switches away from our application,
1292 * automatically release the keyboard back to the system.
1295 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1298 mprintf(( "SetCooperativeLevel failed!\n" ));
1304 // Turn on buffering
1305 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1306 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1308 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1309 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1311 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1313 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1318 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1319 Assert(Di_event != NULL);
1321 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1322 Assert( Di_thread != NULL );
1324 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1326 hr = Di_keyboard->SetEventNotification(Di_event);
1328 mprintf(( "SetEventNotification failed\n" ));
1332 Di_keyboard->Acquire();
1340 * Destroy any lingering IDirectInputDevice object.
1345 * Cleanliness is next to godliness. Unacquire the device
1346 * one last time just in case we got really confused and tried
1347 * to exit while the device is still acquired.
1349 Di_keyboard->Unacquire();
1351 Di_keyboard->Release();
1356 * Destroy any lingering IDirectInput object.
1359 Di_object->Release();
1364 CloseHandle(Di_event);