2 * $Logfile: /Freespace2/code/Io/Key.cpp $
7 * <insert description of file here>
10 * Revision 1.3 2002/05/30 16:50:24 theoddone33
11 * Keyboard partially fixed
13 * Revision 1.2 2002/05/29 23:17:50 theoddone33
14 * Non working text code and fixed keys
16 * Revision 1.1.1.1 2002/05/03 03:28:09 root
20 * 6 10/29/99 6:10p Jefff
21 * squashed the damned y/z german issues once and for all
23 * 5 6/07/99 1:21p Dave
24 * Fixed debug console scrolling problem. Thread related.
26 * 4 6/02/99 6:18p Dave
27 * Fixed TNT lockup problems! Wheeeee!
29 * 3 11/05/98 4:18p Dave
30 * First run nebula support. Beefed up localization a bit. Removed all
31 * conditional compiles for foreign versions. Modified mission file
34 * 2 10/07/98 10:53a Dave
37 * 1 10/07/98 10:49a Dave
39 * 37 6/19/98 3:50p Lawrance
40 * change GERMAN to GR_BUILD
42 * 36 6/17/98 11:05a Lawrance
43 * translate french and german keys
45 * 35 6/12/98 4:49p Hoffoss
46 * Added code to remap scancodes for german and french keyboards.
48 * 34 5/20/98 12:10a Mike
51 * 33 5/19/98 12:19p Mike
54 * 32 5/19/98 12:28a Mike
57 * 31 5/18/98 11:01p Mike
58 * Adding support for cheat system.
60 * 30 5/11/98 12:09a Lawrance
61 * Put in code to turn on/off NumLock key when running under 95
63 * 29 5/01/98 4:23p Lawrance
64 * Remap the scancode for the UK "\" key
66 * 28 4/18/98 12:42p John
67 * Added code to use DirectInput to read keyboard. Took out because it
68 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
70 * 27 4/13/98 10:16a John
71 * Switched gettime back to timer_get_milliseconds, which is now thread
74 * 26 4/12/98 11:08p Lawrance
75 * switch back to using gettime() in separate threads
77 * 25 4/12/98 5:31p Lawrance
78 * use timer_get_milliseconds() instead of gettime()
80 * 24 3/25/98 8:08p John
81 * Restructured software rendering into two modules; One for windowed
82 * debug mode and one for DirectX fullscreen.
84 * 23 1/23/98 3:49p Mike
85 * Fix bug in negative time-down due to latency.
87 * 22 1/07/98 6:41p Lawrance
88 * Pass message latency to the keyboard lib.
90 * 21 11/17/97 10:42a John
91 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
92 * happened, so they're not stuck down.
94 * 20 11/14/97 4:33p Mike
95 * Change Debug key to backquote (from F11).
96 * Balance a ton of subsystems in ships.tbl.
97 * Change "Heavy Laser" to "Disruptor".
99 * 19 9/13/97 9:30a Lawrance
100 * added ability to block certain keys from the keyboard
102 * 18 9/10/97 6:02p Hoffoss
103 * Added code to check for key-pressed sexp operator in FreeSpace as part
104 * of training mission stuff.
106 * 17 9/09/97 11:08a Sandeep
107 * fixed warning level 4
109 * 16 7/29/97 5:30p Lawrance
110 * move gettime() to timer module
112 * 15 4/22/97 10:56a John
113 * fixed some resource leaks.
115 * 14 2/03/97 4:23p Allender
116 * use F11 as debug key now
118 * 13 1/10/97 5:15p Mike
119 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
121 * Added turret code to AI system.
126 //#define USE_DIRECTINPUT
130 #include <windowsx.h>
133 #include <ctype.h> // for toupper
139 #include "localize.h"
141 #define KEY_BUFFER_SIZE 16
143 //-------- Variable accessed by outside functions ---------
144 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
146 uint keyd_last_pressed;
147 uint keyd_last_released;
148 ubyte keyd_pressed[NUM_KEYS];
149 int keyd_time_when_last_pressed;
151 typedef struct keyboard {
152 ushort keybuffer[KEY_BUFFER_SIZE];
153 uint time_pressed[KEY_BUFFER_SIZE];
154 uint TimeKeyWentDown[NUM_KEYS];
155 uint TimeKeyHeldDown[NUM_KEYS];
156 uint TimeKeyDownChecked[NUM_KEYS];
157 uint NumDowns[NUM_KEYS];
158 uint NumUps[NUM_KEYS];
159 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
160 uint keyhead, keytail;
167 CRITICAL_SECTION key_lock;
169 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
170 // This flag was created since the backspace key is also used to correct mistakes
171 // when typing in your pilots callsign. This global flag is checked before execution
174 int ascii_table[128] =
175 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
176 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
177 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
178 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
179 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
180 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
181 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
182 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
183 255,255,255,255,255,255,255,255 };
185 int shifted_ascii_table[128] =
186 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',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', ':', '"', '~',
189 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,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 // used to limit the keypresses that are accepted from the keyboard
197 #define MAX_FILTER_KEYS 64
199 int Key_filter[MAX_FILTER_KEYS];
201 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
202 static int Key_running_NT = 0; // NT is the OS
204 int Cheats_enabled = 0;
205 int Key_normal_game = 0;
207 int key_numlock_is_on()
211 SDL_GetKeyState(keys);
212 if ( keys[SDLK_NUMLOCK] ) {
216 unsigned char keys[256];
217 GetKeyboardState(keys);
218 if ( keys[VK_NUMLOCK] ) {
225 void key_turn_off_numlock()
230 unsigned char keys[256];
231 GetKeyboardState(keys);
232 keys[VK_NUMLOCK] = 0;
233 SetKeyboardState(keys);
237 void key_turn_on_numlock()
242 unsigned char keys[256];
243 GetKeyboardState(keys);
244 keys[VK_NUMLOCK] = 1;
245 SetKeyboardState(keys);
249 // Convert a BIOS scancode to ASCII.
250 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
251 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
252 int key_to_ascii(int keycode )
256 if ( !key_inited ) return 255;
258 shifted = keycode & KEY_SHIFTED;
270 return toupper (keycode);
275 return shifted_ascii_table[keycode];
277 return ascii_table[keycode];
281 // Flush the keyboard buffer.
282 // Clear the keyboard array (keyd_pressed).
288 if ( !key_inited ) return;
293 ENTER_CRITICAL_SECTION(&key_lock);
295 key_data.keyhead = key_data.keytail = 0;
297 //Clear the keyboard buffer
298 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
299 key_data.keybuffer[i] = 0;
300 key_data.time_pressed[i] = 0;
303 //Clear the keyboard array
305 CurTime = timer_get_milliseconds();
308 for (i=0; i<NUM_KEYS; i++ ) {
310 key_data.TimeKeyDownChecked[i] = CurTime;
311 key_data.TimeKeyWentDown[i] = CurTime;
312 key_data.TimeKeyHeldDown[i] = 0;
313 key_data.NumDowns[i]=0;
314 key_data.NumUps[i]=0;
317 LEAVE_CRITICAL_SECTION(&key_lock);
321 // A nifty function which performs the function:
322 // n = (n+1) % KEY_BUFFER_SIZE
323 // (assuming positive values of n).
327 if ( n >= KEY_BUFFER_SIZE ) n=0;
331 // Returns 1 if character waiting... 0 otherwise
334 int is_one_waiting = 0;
336 if ( !key_inited ) return 0;
338 ENTER_CRITICAL_SECTION(&key_lock);
340 if (key_data.keytail != key_data.keyhead){
344 LEAVE_CRITICAL_SECTION(&key_lock);
346 return is_one_waiting;
349 // Return key scancode if a key has been pressed,
351 // Reads keys out of the key buffer and updates keyhead.
356 if ( !key_inited ) return 0;
358 ENTER_CRITICAL_SECTION(&key_lock);
360 if (key_data.keytail!=key_data.keyhead) {
361 key = key_data.keybuffer[key_data.keyhead];
362 key_data.keyhead = add_one(key_data.keyhead);
365 LEAVE_CRITICAL_SECTION(&key_lock);
370 // Unget a key. Puts it back in the input queue.
371 void key_outkey(int key)
375 if ( !key_inited ) return;
377 ENTER_CRITICAL_SECTION(&key_lock);
379 bufp = key_data.keytail+1;
381 if (bufp >= KEY_BUFFER_SIZE){
385 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
387 key_data.keytail = bufp;
389 LEAVE_CRITICAL_SECTION(&key_lock);
394 // Return amount of time last key was held down.
395 // This is currently (July 17, 1996) bogus because our timing is
397 int key_inkey_time(uint * time)
406 ENTER_CRITICAL_SECTION(&key_lock);
408 if (key_data.keytail!=key_data.keyhead) {
409 key = key_data.keybuffer[key_data.keyhead];
410 *time = key_data.time_pressed[key_data.keyhead];
411 key_data.keyhead = add_one(key_data.keyhead);
414 LEAVE_CRITICAL_SECTION(&key_lock);
420 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
421 // but does not update keyhead pointer.
426 if ( !key_inited ) return 0;
428 ENTER_CRITICAL_SECTION(&key_lock);
430 if (key_data.keytail!=key_data.keyhead) {
431 key = key_data.keybuffer[key_data.keyhead];
433 LEAVE_CRITICAL_SECTION(&key_lock);
438 // If not installed, uses BIOS and returns getch();
439 // Else returns pending key (or waits for one if none waiting).
445 if ( !key_inited ) return 0;
447 while (!key_checkch()){
457 // Set global shift_status with modifier results (shift, ctrl, alt).
458 uint key_get_shift_status()
460 unsigned int shift_status = 0;
462 if ( !key_inited ) return 0;
464 ENTER_CRITICAL_SECTION(&key_lock);
466 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
467 shift_status |= KEY_SHIFTED;
469 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
470 shift_status |= KEY_ALTED;
472 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
473 shift_status |= KEY_CTRLED;
476 if (keyd_pressed[KEY_DEBUG_KEY])
477 shift_status |= KEY_DEBUGGED;
479 if (keyd_pressed[KEY_DEBUG_KEY]) {
480 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
481 if ((Cheats_enabled) && Key_normal_game) {
482 mprintf(("Debug key\n"));
483 shift_status |= KEY_DEBUGGED1;
487 LEAVE_CRITICAL_SECTION(&key_lock);
492 // Returns amount of time key (specified by "code") has been down since last call.
493 // Returns float, unlike key_down_time() which returns a fix.
494 float key_down_timef(uint scancode)
496 uint time_down, time;
499 if ( !key_inited ) return 0.0f;
501 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
503 ENTER_CRITICAL_SECTION(&key_lock);
505 time = timer_get_milliseconds();
506 delta_time = time - key_data.TimeKeyDownChecked[scancode];
507 key_data.TimeKeyDownChecked[scancode] = time;
509 if ( delta_time <= 1 ) {
510 key_data.TimeKeyWentDown[scancode] = time;
511 if (keyd_pressed[scancode]) {
512 LEAVE_CRITICAL_SECTION(&key_lock);
515 LEAVE_CRITICAL_SECTION(&key_lock);
520 if ( !keyd_pressed[scancode] ) {
521 time_down = key_data.TimeKeyHeldDown[scancode];
522 key_data.TimeKeyHeldDown[scancode] = 0;
524 time_down = time - key_data.TimeKeyWentDown[scancode];
525 key_data.TimeKeyWentDown[scancode] = time;
528 LEAVE_CRITICAL_SECTION(&key_lock);
530 return i2fl(time_down) / i2fl(delta_time);
534 // Returns amount of time key (specified by "code") has been down since last call.
535 // Returns float, unlike key_down_time() which returns a fix.
536 fix key_down_time( uint code )
538 uint time_down, time;
541 if ( !key_inited ) return 0.0f;
543 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
545 EnterCriticalSection( &key_lock );
547 time = timer_get_milliseconds();
548 delta_time = time - TimeKeyDownChecked[scancode];
549 TimeKeyDownChecked[scancode] = time;
551 if ( delta_time <= 1 ) {
552 LeaveCriticalSection( &key_lock );
553 if (keyd_pressed[scancode])
559 if ( !keyd_pressed[scancode] ) {
560 time_down = key_data.TimeKeyHeldDown[scancode];
561 key_data.TimeKeyHeldDown[scancode] = 0;
563 time_down = time - key_data.TimeKeyWentDown[scancode];
564 key_data.TimeKeyWentDown[scancode] = time;
567 LeaveCriticalSection( &key_lock );
569 return fixmuldiv( time_down, F1_0, delta_time );
574 // Returns number of times key has went from up to down since last call.
575 int key_down_count(int scancode)
579 if ( !key_inited ) return 0;
580 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
582 ENTER_CRITICAL_SECTION(&key_lock);
584 n = key_data.NumDowns[scancode];
585 key_data.NumDowns[scancode] = 0;
587 LEAVE_CRITICAL_SECTION(&key_lock);
593 // Returns number of times key has went from down to up since last call.
594 int key_up_count(int scancode)
598 if ( !key_inited ) return 0;
599 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
601 ENTER_CRITICAL_SECTION(&key_lock);
603 n = key_data.NumUps[scancode];
604 key_data.NumUps[scancode] = 0;
606 LEAVE_CRITICAL_SECTION(&key_lock);
611 int key_check(int key)
613 return key_data.down_check[key];
616 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
617 // latency => time difference in ms between when key was actually pressed and now
618 //void key_mark( uint code, int state )
619 void key_mark( uint code, int state, uint latency )
621 uint scancode, breakbit, temp, event_time;
624 if ( !key_inited ) return;
626 ENTER_CRITICAL_SECTION(&key_lock);
628 // If running in the UK, need to translate their wacky slash scancode to ours
629 if ( code == KEY_SLASH_UK ) {
676 if ( (code == 0xc5) && !Key_running_NT ) {
677 key_turn_off_numlock();
680 Assert( code < NUM_KEYS );
682 event_time = timer_get_milliseconds() - latency;
683 // event_time = timeGetTime() - latency;
687 scancode = code & (NUM_KEYS-1);
695 keyd_last_released = scancode;
696 keyd_pressed[scancode] = 0;
697 key_data.NumUps[scancode]++;
699 // What is the point of this code? "temp" is never used!
701 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
702 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
703 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
705 temp |= keyd_pressed[KEY_DEBUG_KEY];
707 if (event_time < key_data.TimeKeyWentDown[scancode])
708 key_data.TimeKeyHeldDown[scancode] = 0;
710 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
713 keyd_last_pressed = scancode;
714 keyd_time_when_last_pressed = event_time;
715 if (!keyd_pressed[scancode]) {
717 key_data.TimeKeyWentDown[scancode] = event_time;
718 keyd_pressed[scancode] = 1;
719 key_data.NumDowns[scancode]++;
720 key_data.down_check[scancode]++;
722 // mprintf(( "Scancode = %x\n", scancode ));
724 // if ( scancode == KEY_BREAK )
728 } else if (!keyd_repeat) {
729 // Don't buffer repeating key if repeat mode is off
733 if ( scancode!=0xAA ) {
734 keycode = (unsigned short)scancode;
736 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
737 keycode |= KEY_SHIFTED;
739 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
740 keycode |= KEY_ALTED;
742 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
743 keycode |= KEY_CTRLED;
746 if ( keyd_pressed[KEY_DEBUG_KEY] )
747 keycode |= KEY_DEBUGGED;
748 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
750 // keyd_pressed[KEY_DEBUG_KEY] = 0;
751 // keyd_pressed[KEY_BACKSP] = 0;
755 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
756 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
757 if (Cheats_enabled && Key_normal_game) {
758 keycode |= KEY_DEBUGGED1;
765 temp = key_data.keytail+1;
766 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
768 if (temp!=key_data.keyhead) {
769 int i, accept_key = 1;
770 // Num_filter_keys will only be non-zero when a key filter has
771 // been explicity set up via key_set_filter()
772 for ( i = 0; i < Num_filter_keys; i++ ) {
774 if ( Key_filter[i] == keycode ) {
781 key_data.keybuffer[key_data.keytail] = keycode;
782 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
783 key_data.keytail = temp;
790 LEAVE_CRITICAL_SECTION(&key_lock);
793 #ifdef USE_DIRECTINPUT
801 if ( !key_inited ) return;
803 #ifdef USE_DIRECTINPUT
807 if ( Key_numlock_was_on ) {
808 key_turn_on_numlock();
809 Key_numlock_was_on = 0;
816 DeleteCriticalSection( &key_lock );
823 if ( key_inited ) return;
829 InitializeCriticalSection( &key_lock );
831 ENTER_CRITICAL_SECTION(&key_lock);
834 keyd_time_when_last_pressed = timer_get_milliseconds();
835 keyd_buffer_type = 1;
838 // Clear the keyboard array
847 LEAVE_CRITICAL_SECTION(&key_lock);
849 #ifdef USE_DIRECTINPUT
854 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
856 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
860 if ( key_numlock_is_on() ) {
861 Key_numlock_was_on = 1;
862 key_turn_off_numlock();
870 void key_level_init()
874 for (i=0; i<NUM_KEYS; i++)
875 key_data.down_check[i] = 0;
878 void key_lost_focus()
880 if ( !key_inited ) return;
887 if ( !key_inited ) return;
892 // Restricts the keys that are accepted from the keyboard
894 // filter_array => array of keys to act as a filter
895 // num => number of keys in filter_array
897 void key_set_filter(int *filter_array, int num)
901 if ( num >= MAX_FILTER_KEYS ) {
903 num = MAX_FILTER_KEYS;
906 Num_filter_keys = num;
908 for ( i = 0; i < num; i++ ) {
909 Key_filter[i] = filter_array[i];
913 // Clear the key filter, so all keypresses are accepted from keyboard
915 void key_clear_filter()
920 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
926 #ifdef USE_DIRECTINPUT
928 // JAS - April 18, 1998
929 // Not using because DI has the following problems: (Everything else works ok)
930 // Under NT, Pause and Numlock report as identical keys.
931 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
935 //============================================================================
937 // For the keyboard, this basically replaces our old functionallity of:
942 //============================================================================
947 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
949 static LPDIRECTINPUT Di_object = NULL;
950 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
951 static HANDLE Di_thread = NULL;
952 static DWORD Di_thread_id = NULL;
953 static HANDLE Di_event = NULL;
955 DWORD di_process(DWORD lparam)
958 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
960 //mprintf(( "Got event!\n" ));
964 DIDEVICEOBJECTDATA rgdod[10];
965 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
968 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
970 if (hr == DIERR_INPUTLOST) {
972 * DirectInput is telling us that the input stream has
973 * been interrupted. We aren't tracking any state
974 * between polls, so we don't have any special reset
975 * that needs to be done. We just re-acquire and
978 Sleep(1000); // Pause a second...
979 hr = Di_keyboard->Acquire();
986 // dwItems = number of elements read (could be zero)
987 if (hr == DI_BUFFEROVERFLOW) {
988 // Buffer had overflowed.
989 mprintf(( "Buffer overflowed!\n" ));
993 //mprintf(( "Got %d events\n", dwItems ));
995 for (i=0; i<(int)dwItems; i++ ) {
996 int key = rgdod[i].dwOfs;
997 int state = rgdod[i].dwData;
998 int stamp = rgdod[i].dwTimeStamp;
1001 latency = timeGetTime() - stamp;
1005 // if ( key == KEY_PRINT_SCRN ) {
1006 // key_mark( key, 1, latency );
1008 // key_mark( key, (state&0x80?1:0), latency );
1009 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1029 * Register with the DirectInput subsystem and get a pointer
1030 * to a IDirectInput interface we can use.
1036 * Instance handle to our application or DLL.
1038 * DIRECTINPUT_VERSION
1040 * The version of DirectInput we were designed for.
1041 * We take the value from the <dinput.h> header file.
1045 * Receives pointer to the IDirectInput interface
1050 * We do not use OLE aggregation, so this parameter
1054 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1057 mprintf(( "DirectInputCreate failed!\n" ));
1062 * Obtain an interface to the system keyboard device.
1068 * The instance GUID for the device we wish to access.
1069 * GUID_SysKeyboard is a predefined instance GUID that
1070 * always refers to the system keyboard device.
1074 * Receives pointer to the IDirectInputDevice interface
1079 * We do not use OLE aggregation, so this parameter
1083 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1086 mprintf(( "CreateDevice failed!\n" ));
1091 * Set the data format to "keyboard format".
1093 * A data format specifies which controls on a device we
1094 * are interested in, and how they should be reported.
1096 * This tells DirectInput that we will be passing an array
1097 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1103 * Predefined data format which describes
1104 * an array of 256 bytes, one per scancode.
1106 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1109 mprintf(( "SetDataFormat failed!\n" ));
1115 * Set the cooperativity level to let DirectInput know how
1116 * this device should interact with the system and with other
1117 * DirectInput applications.
1121 * DISCL_NONEXCLUSIVE
1123 * Retrieve keyboard data when acquired, not interfering
1124 * with any other applications which are reading keyboard
1129 * If the user switches away from our application,
1130 * automatically release the keyboard back to the system.
1133 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1136 mprintf(( "SetCooperativeLevel failed!\n" ));
1142 // Turn on buffering
1143 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1144 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1146 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1147 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1149 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1151 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1156 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1157 Assert(Di_event != NULL);
1159 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1160 Assert( Di_thread != NULL );
1162 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1164 hr = Di_keyboard->SetEventNotification(Di_event);
1166 mprintf(( "SetEventNotification failed\n" ));
1170 Di_keyboard->Acquire();
1178 * Destroy any lingering IDirectInputDevice object.
1183 * Cleanliness is next to godliness. Unacquire the device
1184 * one last time just in case we got really confused and tried
1185 * to exit while the device is still acquired.
1187 Di_keyboard->Unacquire();
1189 Di_keyboard->Release();
1194 * Destroy any lingering IDirectInput object.
1197 Di_object->Release();
1202 CloseHandle(Di_event);