2 * $Logfile: /Freespace2/code/Io/Key.cpp $
7 * <insert description of file here>
10 * Revision 1.5 2002/05/31 03:34:02 theoddone33
14 * Revision 1.4 2002/05/30 23:46:29 theoddone33
15 * some minor key changes (not necessarily fixes)
17 * Revision 1.3 2002/05/30 16:50:24 theoddone33
18 * Keyboard partially fixed
20 * Revision 1.2 2002/05/29 23:17:50 theoddone33
21 * Non working text code and fixed keys
23 * Revision 1.1.1.1 2002/05/03 03:28:09 root
27 * 6 10/29/99 6:10p Jefff
28 * squashed the damned y/z german issues once and for all
30 * 5 6/07/99 1:21p Dave
31 * Fixed debug console scrolling problem. Thread related.
33 * 4 6/02/99 6:18p Dave
34 * Fixed TNT lockup problems! Wheeeee!
36 * 3 11/05/98 4:18p Dave
37 * First run nebula support. Beefed up localization a bit. Removed all
38 * conditional compiles for foreign versions. Modified mission file
41 * 2 10/07/98 10:53a Dave
44 * 1 10/07/98 10:49a Dave
46 * 37 6/19/98 3:50p Lawrance
47 * change GERMAN to GR_BUILD
49 * 36 6/17/98 11:05a Lawrance
50 * translate french and german keys
52 * 35 6/12/98 4:49p Hoffoss
53 * Added code to remap scancodes for german and french keyboards.
55 * 34 5/20/98 12:10a Mike
58 * 33 5/19/98 12:19p Mike
61 * 32 5/19/98 12:28a Mike
64 * 31 5/18/98 11:01p Mike
65 * Adding support for cheat system.
67 * 30 5/11/98 12:09a Lawrance
68 * Put in code to turn on/off NumLock key when running under 95
70 * 29 5/01/98 4:23p Lawrance
71 * Remap the scancode for the UK "\" key
73 * 28 4/18/98 12:42p John
74 * Added code to use DirectInput to read keyboard. Took out because it
75 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
77 * 27 4/13/98 10:16a John
78 * Switched gettime back to timer_get_milliseconds, which is now thread
81 * 26 4/12/98 11:08p Lawrance
82 * switch back to using gettime() in separate threads
84 * 25 4/12/98 5:31p Lawrance
85 * use timer_get_milliseconds() instead of gettime()
87 * 24 3/25/98 8:08p John
88 * Restructured software rendering into two modules; One for windowed
89 * debug mode and one for DirectX fullscreen.
91 * 23 1/23/98 3:49p Mike
92 * Fix bug in negative time-down due to latency.
94 * 22 1/07/98 6:41p Lawrance
95 * Pass message latency to the keyboard lib.
97 * 21 11/17/97 10:42a John
98 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
99 * happened, so they're not stuck down.
101 * 20 11/14/97 4:33p Mike
102 * Change Debug key to backquote (from F11).
103 * Balance a ton of subsystems in ships.tbl.
104 * Change "Heavy Laser" to "Disruptor".
106 * 19 9/13/97 9:30a Lawrance
107 * added ability to block certain keys from the keyboard
109 * 18 9/10/97 6:02p Hoffoss
110 * Added code to check for key-pressed sexp operator in FreeSpace as part
111 * of training mission stuff.
113 * 17 9/09/97 11:08a Sandeep
114 * fixed warning level 4
116 * 16 7/29/97 5:30p Lawrance
117 * move gettime() to timer module
119 * 15 4/22/97 10:56a John
120 * fixed some resource leaks.
122 * 14 2/03/97 4:23p Allender
123 * use F11 as debug key now
125 * 13 1/10/97 5:15p Mike
126 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
128 * Added turret code to AI system.
133 //#define USE_DIRECTINPUT
137 #include <windowsx.h>
140 #include <ctype.h> // for toupper
146 #include "localize.h"
148 #define KEY_BUFFER_SIZE 16
150 //-------- Variable accessed by outside functions ---------
151 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
153 uint keyd_last_pressed;
154 uint keyd_last_released;
155 ubyte keyd_pressed[NUM_KEYS];
156 int keyd_time_when_last_pressed;
158 typedef struct keyboard {
159 ushort keybuffer[KEY_BUFFER_SIZE];
160 uint time_pressed[KEY_BUFFER_SIZE];
161 uint TimeKeyWentDown[NUM_KEYS];
162 uint TimeKeyHeldDown[NUM_KEYS];
163 uint TimeKeyDownChecked[NUM_KEYS];
164 uint NumDowns[NUM_KEYS];
165 uint NumUps[NUM_KEYS];
166 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
167 uint keyhead, keytail;
174 CRITICAL_SECTION key_lock;
176 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
177 // This flag was created since the backspace key is also used to correct mistakes
178 // when typing in your pilots callsign. This global flag is checked before execution
182 int SDLtoFS2[SDLK_LAST];
185 int ascii_table[128] =
186 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
187 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
188 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
189 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
190 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
191 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
192 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
193 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
194 255,255,255,255,255,255,255,255 };
196 int shifted_ascii_table[128] =
197 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
198 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
199 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
200 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
201 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
202 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
203 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
204 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
205 255,255,255,255,255,255,255,255 };
207 // used to limit the keypresses that are accepted from the keyboard
208 #define MAX_FILTER_KEYS 64
210 int Key_filter[MAX_FILTER_KEYS];
212 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
213 static int Key_running_NT = 0; // NT is the OS
215 int Cheats_enabled = 0;
216 int Key_normal_game = 0;
221 SDLtoFS2[SDLK_0] = KEY_0;
222 SDLtoFS2[SDLK_1] = KEY_1;
223 SDLtoFS2[SDLK_2] = KEY_2;
224 SDLtoFS2[SDLK_3] = KEY_3;
225 SDLtoFS2[SDLK_4] = KEY_4;
226 SDLtoFS2[SDLK_5] = KEY_5;
227 SDLtoFS2[SDLK_6] = KEY_6;
228 SDLtoFS2[SDLK_7] = KEY_7;
229 SDLtoFS2[SDLK_8] = KEY_8;
230 SDLtoFS2[SDLK_9] = KEY_9;
232 SDLtoFS2[SDLK_a] = KEY_A;
233 SDLtoFS2[SDLK_b] = KEY_B;
234 SDLtoFS2[SDLK_c] = KEY_C;
235 SDLtoFS2[SDLK_d] = KEY_D;
236 SDLtoFS2[SDLK_e] = KEY_E;
237 SDLtoFS2[SDLK_f] = KEY_F;
238 SDLtoFS2[SDLK_g] = KEY_G;
239 SDLtoFS2[SDLK_h] = KEY_H;
240 SDLtoFS2[SDLK_i] = KEY_I;
241 SDLtoFS2[SDLK_j] = KEY_J;
242 SDLtoFS2[SDLK_k] = KEY_K;
243 SDLtoFS2[SDLK_l] = KEY_L;
244 SDLtoFS2[SDLK_m] = KEY_M;
245 SDLtoFS2[SDLK_n] = KEY_N;
246 SDLtoFS2[SDLK_o] = KEY_O;
247 SDLtoFS2[SDLK_p] = KEY_P;
248 SDLtoFS2[SDLK_q] = KEY_Q;
249 SDLtoFS2[SDLK_r] = KEY_R;
250 SDLtoFS2[SDLK_s] = KEY_S;
251 SDLtoFS2[SDLK_t] = KEY_T;
252 SDLtoFS2[SDLK_u] = KEY_U;
253 SDLtoFS2[SDLK_v] = KEY_V;
254 SDLtoFS2[SDLK_w] = KEY_W;
255 SDLtoFS2[SDLK_x] = KEY_X;
256 SDLtoFS2[SDLK_y] = KEY_Y;
257 SDLtoFS2[SDLK_z] = KEY_Z;
259 SDLtoFS2[SDLK_MINUS] = KEY_MINUS;
260 SDLtoFS2[SDLK_EQUALS] = KEY_EQUAL;
261 SDLtoFS2[SDLK_SLASH] = KEY_DIVIDE; // No idea - DDOI
262 SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH;
263 //SDLtoFS2[SDLK_BACKSLASH] = KEY_SLASH_UK; // ?
264 SDLtoFS2[SDLK_COMMA] = KEY_COMMA;
265 SDLtoFS2[SDLK_PERIOD] = KEY_PERIOD;
266 SDLtoFS2[SDLK_SEMICOLON] = KEY_SEMICOL;
268 SDLtoFS2[SDLK_LEFTBRACKET] = KEY_LBRACKET;
269 SDLtoFS2[SDLK_RIGHTBRACKET] = KEY_RBRACKET;
271 SDLtoFS2[SDLK_BACKQUOTE] = KEY_RAPOSTRO;
272 SDLtoFS2[SDLK_QUOTE] = KEY_LAPOSTRO;
274 SDLtoFS2[SDLK_ESCAPE] = KEY_ESC;
275 SDLtoFS2[SDLK_RETURN] = KEY_ENTER;
276 SDLtoFS2[SDLK_BACKSPACE] = KEY_BACKSP;
277 SDLtoFS2[SDLK_TAB] = KEY_TAB;
278 SDLtoFS2[SDLK_SPACE] = KEY_SPACEBAR;
280 SDLtoFS2[SDLK_NUMLOCK] = KEY_NUMLOCK;
281 SDLtoFS2[SDLK_SCROLLOCK] = KEY_SCROLLOCK;
282 SDLtoFS2[SDLK_CAPSLOCK] = KEY_CAPSLOCK;
284 SDLtoFS2[SDLK_LSHIFT] = KEY_LSHIFT;
285 SDLtoFS2[SDLK_RSHIFT] = KEY_RSHIFT;
287 SDLtoFS2[SDLK_LALT] = KEY_LALT;
288 SDLtoFS2[SDLK_RALT] = KEY_RALT;
290 SDLtoFS2[SDLK_LCTRL] = KEY_LCTRL;
291 SDLtoFS2[SDLK_RCTRL] = KEY_RCTRL;
293 SDLtoFS2[SDLK_F1] = KEY_F1;
294 SDLtoFS2[SDLK_F2] = KEY_F2;
295 SDLtoFS2[SDLK_F3] = KEY_F3;
296 SDLtoFS2[SDLK_F4] = KEY_F4;
297 SDLtoFS2[SDLK_F5] = KEY_F5;
298 SDLtoFS2[SDLK_F6] = KEY_F6;
299 SDLtoFS2[SDLK_F7] = KEY_F7;
300 SDLtoFS2[SDLK_F8] = KEY_F8;
301 SDLtoFS2[SDLK_F9] = KEY_F9;
302 SDLtoFS2[SDLK_F10] = KEY_F10;
303 SDLtoFS2[SDLK_F11] = KEY_F11;
304 SDLtoFS2[SDLK_F12] = KEY_F12;
306 SDLtoFS2[SDLK_KP0] = KEY_PAD0;
307 SDLtoFS2[SDLK_KP1] = KEY_PAD1;
308 SDLtoFS2[SDLK_KP2] = KEY_PAD2;
309 SDLtoFS2[SDLK_KP3] = KEY_PAD3;
310 SDLtoFS2[SDLK_KP4] = KEY_PAD4;
311 SDLtoFS2[SDLK_KP5] = KEY_PAD5;
312 SDLtoFS2[SDLK_KP6] = KEY_PAD6;
313 SDLtoFS2[SDLK_KP7] = KEY_PAD7;
314 SDLtoFS2[SDLK_KP8] = KEY_PAD8;
315 SDLtoFS2[SDLK_KP9] = KEY_PAD9;
316 SDLtoFS2[SDLK_KP_MINUS] = KEY_PADMINUS;
317 SDLtoFS2[SDLK_KP_PLUS] = KEY_PADPLUS;
318 SDLtoFS2[SDLK_KP_PERIOD] = KEY_PADPERIOD;
319 SDLtoFS2[SDLK_KP_DIVIDE] = KEY_PADDIVIDE;
320 SDLtoFS2[SDLK_KP_MULTIPLY] = KEY_PADMULTIPLY;
321 SDLtoFS2[SDLK_KP_ENTER] = KEY_PADENTER;
323 SDLtoFS2[SDLK_INSERT] = KEY_INSERT;
324 SDLtoFS2[SDLK_HOME] = KEY_HOME;
325 SDLtoFS2[SDLK_PAGEUP] = KEY_PAGEUP;
326 SDLtoFS2[SDLK_DELETE] = KEY_DELETE;
327 SDLtoFS2[SDLK_END] = KEY_END;
328 SDLtoFS2[SDLK_PAGEDOWN] = KEY_PAGEDOWN;
329 SDLtoFS2[SDLK_UP] = KEY_UP;
330 SDLtoFS2[SDLK_DOWN] = KEY_DOWN;
331 SDLtoFS2[SDLK_LEFT] = KEY_LEFT;
332 SDLtoFS2[SDLK_RIGHT] = KEY_RIGHT;
334 SDLtoFS2[SDLK_PRINT] = KEY_PRINT_SCRN;
335 SDLtoFS2[SDLK_PAUSE] = KEY_PAUSE;
336 SDLtoFS2[SDLK_BREAK] = KEY_BREAK;
340 int key_numlock_is_on()
344 SDL_GetKeyState(keys);
345 if ( keys[SDLK_NUMLOCK] ) {
349 unsigned char keys[256];
350 GetKeyboardState(keys);
351 if ( keys[VK_NUMLOCK] ) {
358 void key_turn_off_numlock()
363 unsigned char keys[256];
364 GetKeyboardState(keys);
365 keys[VK_NUMLOCK] = 0;
366 SetKeyboardState(keys);
370 void key_turn_on_numlock()
375 unsigned char keys[256];
376 GetKeyboardState(keys);
377 keys[VK_NUMLOCK] = 1;
378 SetKeyboardState(keys);
382 // Convert a BIOS scancode to ASCII.
383 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
384 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
385 int key_to_ascii(int keycode )
389 if ( !key_inited ) return 255;
391 shifted = keycode & KEY_SHIFTED;
398 return shifted_ascii_table[keycode];
400 return ascii_table[keycode];
403 // Flush the keyboard buffer.
404 // Clear the keyboard array (keyd_pressed).
410 if ( !key_inited ) return;
412 ENTER_CRITICAL_SECTION(&key_lock);
414 key_data.keyhead = key_data.keytail = 0;
416 //Clear the keyboard buffer
417 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
418 key_data.keybuffer[i] = 0;
419 key_data.time_pressed[i] = 0;
422 //Clear the keyboard array
424 CurTime = timer_get_milliseconds();
427 for (i=0; i<NUM_KEYS; i++ ) {
429 key_data.TimeKeyDownChecked[i] = CurTime;
430 key_data.TimeKeyWentDown[i] = CurTime;
431 key_data.TimeKeyHeldDown[i] = 0;
432 key_data.NumDowns[i]=0;
433 key_data.NumUps[i]=0;
436 LEAVE_CRITICAL_SECTION(&key_lock);
439 // A nifty function which performs the function:
440 // n = (n+1) % KEY_BUFFER_SIZE
441 // (assuming positive values of n).
445 if ( n >= KEY_BUFFER_SIZE ) n=0;
449 // Returns 1 if character waiting... 0 otherwise
452 int is_one_waiting = 0;
454 if ( !key_inited ) return 0;
456 ENTER_CRITICAL_SECTION(&key_lock);
458 if (key_data.keytail != key_data.keyhead){
462 LEAVE_CRITICAL_SECTION(&key_lock);
464 return is_one_waiting;
467 // Return key scancode if a key has been pressed,
469 // Reads keys out of the key buffer and updates keyhead.
474 if ( !key_inited ) return 0;
476 ENTER_CRITICAL_SECTION(&key_lock);
478 if (key_data.keytail!=key_data.keyhead) {
479 key = key_data.keybuffer[key_data.keyhead];
480 key_data.keyhead = add_one(key_data.keyhead);
483 LEAVE_CRITICAL_SECTION(&key_lock);
488 // Unget a key. Puts it back in the input queue.
489 void key_outkey(int key)
493 if ( !key_inited ) return;
495 ENTER_CRITICAL_SECTION(&key_lock);
497 bufp = key_data.keytail+1;
499 if (bufp >= KEY_BUFFER_SIZE){
503 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
505 key_data.keytail = bufp;
507 LEAVE_CRITICAL_SECTION(&key_lock);
512 // Return amount of time last key was held down.
513 // This is currently (July 17, 1996) bogus because our timing is
515 int key_inkey_time(uint * time)
524 ENTER_CRITICAL_SECTION(&key_lock);
526 if (key_data.keytail!=key_data.keyhead) {
527 key = key_data.keybuffer[key_data.keyhead];
528 *time = key_data.time_pressed[key_data.keyhead];
529 key_data.keyhead = add_one(key_data.keyhead);
532 LEAVE_CRITICAL_SECTION(&key_lock);
538 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
539 // but does not update keyhead pointer.
544 if ( !key_inited ) return 0;
546 ENTER_CRITICAL_SECTION(&key_lock);
548 if (key_data.keytail!=key_data.keyhead) {
549 key = key_data.keybuffer[key_data.keyhead];
551 LEAVE_CRITICAL_SECTION(&key_lock);
556 // If not installed, uses BIOS and returns getch();
557 // Else returns pending key (or waits for one if none waiting).
563 if ( !key_inited ) return 0;
565 while (!key_checkch()){
575 // Set global shift_status with modifier results (shift, ctrl, alt).
576 uint key_get_shift_status()
578 unsigned int shift_status = 0;
580 if ( !key_inited ) return 0;
582 ENTER_CRITICAL_SECTION(&key_lock);
584 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
585 shift_status |= KEY_SHIFTED;
587 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
588 shift_status |= KEY_ALTED;
590 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
591 shift_status |= KEY_CTRLED;
594 if (keyd_pressed[KEY_DEBUG_KEY])
595 shift_status |= KEY_DEBUGGED;
597 if (keyd_pressed[KEY_DEBUG_KEY]) {
598 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
599 if ((Cheats_enabled) && Key_normal_game) {
600 mprintf(("Debug key\n"));
601 shift_status |= KEY_DEBUGGED1;
605 LEAVE_CRITICAL_SECTION(&key_lock);
610 // Returns amount of time key (specified by "code") has been down since last call.
611 // Returns float, unlike key_down_time() which returns a fix.
612 float key_down_timef(uint scancode)
614 uint time_down, time;
617 if ( !key_inited ) return 0.0f;
619 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
621 ENTER_CRITICAL_SECTION(&key_lock);
623 time = timer_get_milliseconds();
624 delta_time = time - key_data.TimeKeyDownChecked[scancode];
625 key_data.TimeKeyDownChecked[scancode] = time;
627 if ( delta_time <= 1 ) {
628 key_data.TimeKeyWentDown[scancode] = time;
629 if (keyd_pressed[scancode]) {
630 LEAVE_CRITICAL_SECTION(&key_lock);
633 LEAVE_CRITICAL_SECTION(&key_lock);
638 if ( !keyd_pressed[scancode] ) {
639 time_down = key_data.TimeKeyHeldDown[scancode];
640 key_data.TimeKeyHeldDown[scancode] = 0;
642 time_down = time - key_data.TimeKeyWentDown[scancode];
643 key_data.TimeKeyWentDown[scancode] = time;
646 LEAVE_CRITICAL_SECTION(&key_lock);
648 return i2fl(time_down) / i2fl(delta_time);
652 // Returns amount of time key (specified by "code") has been down since last call.
653 // Returns float, unlike key_down_time() which returns a fix.
654 fix key_down_time( uint code )
656 uint time_down, time;
659 if ( !key_inited ) return 0.0f;
661 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
663 EnterCriticalSection( &key_lock );
665 time = timer_get_milliseconds();
666 delta_time = time - TimeKeyDownChecked[scancode];
667 TimeKeyDownChecked[scancode] = time;
669 if ( delta_time <= 1 ) {
670 LeaveCriticalSection( &key_lock );
671 if (keyd_pressed[scancode])
677 if ( !keyd_pressed[scancode] ) {
678 time_down = key_data.TimeKeyHeldDown[scancode];
679 key_data.TimeKeyHeldDown[scancode] = 0;
681 time_down = time - key_data.TimeKeyWentDown[scancode];
682 key_data.TimeKeyWentDown[scancode] = time;
685 LeaveCriticalSection( &key_lock );
687 return fixmuldiv( time_down, F1_0, delta_time );
692 // Returns number of times key has went from up to down since last call.
693 int key_down_count(int scancode)
697 if ( !key_inited ) return 0;
699 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
701 ENTER_CRITICAL_SECTION(&key_lock);
703 n = key_data.NumDowns[scancode];
704 key_data.NumDowns[scancode] = 0;
706 LEAVE_CRITICAL_SECTION(&key_lock);
712 // Returns number of times key has went from down to up since last call.
713 int key_up_count(int scancode)
717 if ( !key_inited ) return 0;
718 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
720 ENTER_CRITICAL_SECTION(&key_lock);
722 n = key_data.NumUps[scancode];
723 key_data.NumUps[scancode] = 0;
725 LEAVE_CRITICAL_SECTION(&key_lock);
730 int key_check(int key)
732 return key_data.down_check[key];
735 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
736 // latency => time difference in ms between when key was actually pressed and now
737 //void key_mark( uint code, int state )
738 void key_mark( uint code, int state, uint latency )
740 uint scancode, breakbit, temp, event_time;
743 if ( !key_inited ) return;
745 ENTER_CRITICAL_SECTION(&key_lock);
747 // If running in the UK, need to translate their wacky slash scancode to ours
748 if ( code == KEY_SLASH_UK ) {
795 if ( (code == 0xc5) && !Key_running_NT ) {
796 key_turn_off_numlock();
799 Assert( code < NUM_KEYS );
801 event_time = timer_get_milliseconds() - latency;
802 // event_time = timeGetTime() - latency;
805 scancode = code & (NUM_KEYS-1);
810 keyd_last_released = scancode;
811 keyd_pressed[scancode] = 0;
812 key_data.NumUps[scancode]++;
814 // What is the point of this code? "temp" is never used!
816 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
817 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
818 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
820 temp |= keyd_pressed[KEY_DEBUG_KEY];
822 if (event_time < key_data.TimeKeyWentDown[scancode])
823 key_data.TimeKeyHeldDown[scancode] = 0;
825 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
828 keyd_last_pressed = scancode;
829 keyd_time_when_last_pressed = event_time;
830 if (!keyd_pressed[scancode]) {
832 key_data.TimeKeyWentDown[scancode] = event_time;
833 keyd_pressed[scancode] = 1;
834 key_data.NumDowns[scancode]++;
835 key_data.down_check[scancode]++;
837 // mprintf(( "Scancode = %x\n", scancode ));
839 // if ( scancode == KEY_BREAK )
843 } else if (!keyd_repeat) {
844 // Don't buffer repeating key if repeat mode is off
848 if ( scancode!=0xAA ) {
849 keycode = (unsigned short)scancode;
851 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
852 keycode |= KEY_SHIFTED;
854 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
855 keycode |= KEY_ALTED;
857 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
858 keycode |= KEY_CTRLED;
861 if ( keyd_pressed[KEY_DEBUG_KEY] )
862 keycode |= KEY_DEBUGGED;
863 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
865 // keyd_pressed[KEY_DEBUG_KEY] = 0;
866 // keyd_pressed[KEY_BACKSP] = 0;
870 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
871 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
872 if (Cheats_enabled && Key_normal_game) {
873 keycode |= KEY_DEBUGGED1;
880 temp = key_data.keytail+1;
881 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
883 if (temp!=key_data.keyhead) {
884 int i, accept_key = 1;
885 // Num_filter_keys will only be non-zero when a key filter has
886 // been explicity set up via key_set_filter()
887 for ( i = 0; i < Num_filter_keys; i++ ) {
889 if ( Key_filter[i] == keycode ) {
896 key_data.keybuffer[key_data.keytail] = keycode;
897 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
898 key_data.keytail = temp;
905 LEAVE_CRITICAL_SECTION(&key_lock);
908 #ifdef USE_DIRECTINPUT
916 if ( !key_inited ) return;
918 #ifdef USE_DIRECTINPUT
922 if ( Key_numlock_was_on ) {
923 key_turn_on_numlock();
924 Key_numlock_was_on = 0;
931 DeleteCriticalSection( &key_lock );
938 if ( key_inited ) return;
945 InitializeCriticalSection( &key_lock );
947 ENTER_CRITICAL_SECTION(&key_lock);
950 keyd_time_when_last_pressed = timer_get_milliseconds();
951 keyd_buffer_type = 1;
954 // Clear the keyboard array
963 LEAVE_CRITICAL_SECTION(&key_lock);
965 #ifdef USE_DIRECTINPUT
970 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
972 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
976 if ( key_numlock_is_on() ) {
977 Key_numlock_was_on = 1;
978 key_turn_off_numlock();
986 void key_level_init()
990 for (i=0; i<NUM_KEYS; i++)
991 key_data.down_check[i] = 0;
994 void key_lost_focus()
996 if ( !key_inited ) return;
1001 void key_got_focus()
1003 if ( !key_inited ) return;
1008 // Restricts the keys that are accepted from the keyboard
1010 // filter_array => array of keys to act as a filter
1011 // num => number of keys in filter_array
1013 void key_set_filter(int *filter_array, int num)
1017 if ( num >= MAX_FILTER_KEYS ) {
1019 num = MAX_FILTER_KEYS;
1022 Num_filter_keys = num;
1024 for ( i = 0; i < num; i++ ) {
1025 Key_filter[i] = filter_array[i];
1029 // Clear the key filter, so all keypresses are accepted from keyboard
1031 void key_clear_filter()
1035 Num_filter_keys = 0;
1036 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
1042 #ifdef USE_DIRECTINPUT
1044 // JAS - April 18, 1998
1045 // Not using because DI has the following problems: (Everything else works ok)
1046 // Under NT, Pause and Numlock report as identical keys.
1047 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
1051 //============================================================================
1052 // Direct Input code
1053 // For the keyboard, this basically replaces our old functionallity of:
1058 //============================================================================
1061 #include "vdinput.h"
1063 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
1065 static LPDIRECTINPUT Di_object = NULL;
1066 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
1067 static HANDLE Di_thread = NULL;
1068 static DWORD Di_thread_id = NULL;
1069 static HANDLE Di_event = NULL;
1071 DWORD di_process(DWORD lparam)
1074 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
1076 //mprintf(( "Got event!\n" ));
1080 DIDEVICEOBJECTDATA rgdod[10];
1081 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
1084 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
1086 if (hr == DIERR_INPUTLOST) {
1088 * DirectInput is telling us that the input stream has
1089 * been interrupted. We aren't tracking any state
1090 * between polls, so we don't have any special reset
1091 * that needs to be done. We just re-acquire and
1094 Sleep(1000); // Pause a second...
1095 hr = Di_keyboard->Acquire();
1096 if (SUCCEEDED(hr)) {
1101 if (SUCCEEDED(hr)) {
1102 // dwItems = number of elements read (could be zero)
1103 if (hr == DI_BUFFEROVERFLOW) {
1104 // Buffer had overflowed.
1105 mprintf(( "Buffer overflowed!\n" ));
1109 //mprintf(( "Got %d events\n", dwItems ));
1111 for (i=0; i<(int)dwItems; i++ ) {
1112 int key = rgdod[i].dwOfs;
1113 int state = rgdod[i].dwData;
1114 int stamp = rgdod[i].dwTimeStamp;
1117 latency = timeGetTime() - stamp;
1121 // if ( key == KEY_PRINT_SCRN ) {
1122 // key_mark( key, 1, latency );
1124 // key_mark( key, (state&0x80?1:0), latency );
1125 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1145 * Register with the DirectInput subsystem and get a pointer
1146 * to a IDirectInput interface we can use.
1152 * Instance handle to our application or DLL.
1154 * DIRECTINPUT_VERSION
1156 * The version of DirectInput we were designed for.
1157 * We take the value from the <dinput.h> header file.
1161 * Receives pointer to the IDirectInput interface
1166 * We do not use OLE aggregation, so this parameter
1170 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1173 mprintf(( "DirectInputCreate failed!\n" ));
1178 * Obtain an interface to the system keyboard device.
1184 * The instance GUID for the device we wish to access.
1185 * GUID_SysKeyboard is a predefined instance GUID that
1186 * always refers to the system keyboard device.
1190 * Receives pointer to the IDirectInputDevice interface
1195 * We do not use OLE aggregation, so this parameter
1199 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1202 mprintf(( "CreateDevice failed!\n" ));
1207 * Set the data format to "keyboard format".
1209 * A data format specifies which controls on a device we
1210 * are interested in, and how they should be reported.
1212 * This tells DirectInput that we will be passing an array
1213 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1219 * Predefined data format which describes
1220 * an array of 256 bytes, one per scancode.
1222 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1225 mprintf(( "SetDataFormat failed!\n" ));
1231 * Set the cooperativity level to let DirectInput know how
1232 * this device should interact with the system and with other
1233 * DirectInput applications.
1237 * DISCL_NONEXCLUSIVE
1239 * Retrieve keyboard data when acquired, not interfering
1240 * with any other applications which are reading keyboard
1245 * If the user switches away from our application,
1246 * automatically release the keyboard back to the system.
1249 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1252 mprintf(( "SetCooperativeLevel failed!\n" ));
1258 // Turn on buffering
1259 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1260 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1262 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1263 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1265 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1267 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1272 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1273 Assert(Di_event != NULL);
1275 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1276 Assert( Di_thread != NULL );
1278 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1280 hr = Di_keyboard->SetEventNotification(Di_event);
1282 mprintf(( "SetEventNotification failed\n" ));
1286 Di_keyboard->Acquire();
1294 * Destroy any lingering IDirectInputDevice object.
1299 * Cleanliness is next to godliness. Unacquire the device
1300 * one last time just in case we got really confused and tried
1301 * to exit while the device is still acquired.
1303 Di_keyboard->Unacquire();
1305 Di_keyboard->Release();
1310 * Destroy any lingering IDirectInput object.
1313 Di_object->Release();
1318 CloseHandle(Di_event);