2 * $Logfile: /Freespace2/code/Io/Key.cpp $
7 * <insert description of file here>
10 * Revision 1.2 2002/05/29 23:17:50 theoddone33
11 * Non working text code and fixed keys
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 6 10/29/99 6:10p Jefff
18 * squashed the damned y/z german issues once and for all
20 * 5 6/07/99 1:21p Dave
21 * Fixed debug console scrolling problem. Thread related.
23 * 4 6/02/99 6:18p Dave
24 * Fixed TNT lockup problems! Wheeeee!
26 * 3 11/05/98 4:18p Dave
27 * First run nebula support. Beefed up localization a bit. Removed all
28 * conditional compiles for foreign versions. Modified mission file
31 * 2 10/07/98 10:53a Dave
34 * 1 10/07/98 10:49a Dave
36 * 37 6/19/98 3:50p Lawrance
37 * change GERMAN to GR_BUILD
39 * 36 6/17/98 11:05a Lawrance
40 * translate french and german keys
42 * 35 6/12/98 4:49p Hoffoss
43 * Added code to remap scancodes for german and french keyboards.
45 * 34 5/20/98 12:10a Mike
48 * 33 5/19/98 12:19p Mike
51 * 32 5/19/98 12:28a Mike
54 * 31 5/18/98 11:01p Mike
55 * Adding support for cheat system.
57 * 30 5/11/98 12:09a Lawrance
58 * Put in code to turn on/off NumLock key when running under 95
60 * 29 5/01/98 4:23p Lawrance
61 * Remap the scancode for the UK "\" key
63 * 28 4/18/98 12:42p John
64 * Added code to use DirectInput to read keyboard. Took out because it
65 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
67 * 27 4/13/98 10:16a John
68 * Switched gettime back to timer_get_milliseconds, which is now thread
71 * 26 4/12/98 11:08p Lawrance
72 * switch back to using gettime() in separate threads
74 * 25 4/12/98 5:31p Lawrance
75 * use timer_get_milliseconds() instead of gettime()
77 * 24 3/25/98 8:08p John
78 * Restructured software rendering into two modules; One for windowed
79 * debug mode and one for DirectX fullscreen.
81 * 23 1/23/98 3:49p Mike
82 * Fix bug in negative time-down due to latency.
84 * 22 1/07/98 6:41p Lawrance
85 * Pass message latency to the keyboard lib.
87 * 21 11/17/97 10:42a John
88 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
89 * happened, so they're not stuck down.
91 * 20 11/14/97 4:33p Mike
92 * Change Debug key to backquote (from F11).
93 * Balance a ton of subsystems in ships.tbl.
94 * Change "Heavy Laser" to "Disruptor".
96 * 19 9/13/97 9:30a Lawrance
97 * added ability to block certain keys from the keyboard
99 * 18 9/10/97 6:02p Hoffoss
100 * Added code to check for key-pressed sexp operator in FreeSpace as part
101 * of training mission stuff.
103 * 17 9/09/97 11:08a Sandeep
104 * fixed warning level 4
106 * 16 7/29/97 5:30p Lawrance
107 * move gettime() to timer module
109 * 15 4/22/97 10:56a John
110 * fixed some resource leaks.
112 * 14 2/03/97 4:23p Allender
113 * use F11 as debug key now
115 * 13 1/10/97 5:15p Mike
116 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
118 * Added turret code to AI system.
123 //#define USE_DIRECTINPUT
127 #include <windowsx.h>
135 #include "localize.h"
137 #define KEY_BUFFER_SIZE 16
139 //-------- Variable accessed by outside functions ---------
140 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
142 uint keyd_last_pressed;
143 uint keyd_last_released;
144 ubyte keyd_pressed[NUM_KEYS];
145 int keyd_time_when_last_pressed;
147 typedef struct keyboard {
148 ushort keybuffer[KEY_BUFFER_SIZE];
149 uint time_pressed[KEY_BUFFER_SIZE];
150 uint TimeKeyWentDown[NUM_KEYS];
151 uint TimeKeyHeldDown[NUM_KEYS];
152 uint TimeKeyDownChecked[NUM_KEYS];
153 uint NumDowns[NUM_KEYS];
154 uint NumUps[NUM_KEYS];
155 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
156 uint keyhead, keytail;
163 CRITICAL_SECTION key_lock;
165 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
166 // This flag was created since the backspace key is also used to correct mistakes
167 // when typing in your pilots callsign. This global flag is checked before execution
170 int ascii_table[128] =
171 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
172 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
173 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
174 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
175 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
176 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
177 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
178 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
179 255,255,255,255,255,255,255,255 };
181 int shifted_ascii_table[128] =
182 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
183 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
184 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
185 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
186 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
187 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
188 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
189 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
190 255,255,255,255,255,255,255,255 };
192 // used to limit the keypresses that are accepted from the keyboard
193 #define MAX_FILTER_KEYS 64
195 int Key_filter[MAX_FILTER_KEYS];
197 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
198 static int Key_running_NT = 0; // NT is the OS
200 int Cheats_enabled = 0;
201 int Key_normal_game = 0;
203 int key_numlock_is_on()
207 SDL_GetKeyState(keys);
208 if ( keys[SDLK_NUMLOCK] ) {
212 unsigned char keys[256];
213 GetKeyboardState(keys);
214 if ( keys[VK_NUMLOCK] ) {
221 void key_turn_off_numlock()
226 unsigned char keys[256];
227 GetKeyboardState(keys);
228 keys[VK_NUMLOCK] = 0;
229 SetKeyboardState(keys);
233 void key_turn_on_numlock()
238 unsigned char keys[256];
239 GetKeyboardState(keys);
240 keys[VK_NUMLOCK] = 1;
241 SetKeyboardState(keys);
245 // Convert a BIOS scancode to ASCII.
246 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
247 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
248 int key_to_ascii(int keycode )
252 if ( !key_inited ) return 255;
254 shifted = keycode & KEY_SHIFTED;
261 return shifted_ascii_table[keycode];
263 return ascii_table[keycode];
266 // Flush the keyboard buffer.
267 // Clear the keyboard array (keyd_pressed).
273 if ( !key_inited ) return;
278 ENTER_CRITICAL_SECTION(&key_lock);
280 key_data.keyhead = key_data.keytail = 0;
282 //Clear the keyboard buffer
283 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
284 key_data.keybuffer[i] = 0;
285 key_data.time_pressed[i] = 0;
288 //Clear the keyboard array
290 CurTime = timer_get_milliseconds();
293 for (i=0; i<NUM_KEYS; i++ ) {
295 key_data.TimeKeyDownChecked[i] = CurTime;
296 key_data.TimeKeyWentDown[i] = CurTime;
297 key_data.TimeKeyHeldDown[i] = 0;
298 key_data.NumDowns[i]=0;
299 key_data.NumUps[i]=0;
302 LEAVE_CRITICAL_SECTION(&key_lock);
306 // A nifty function which performs the function:
307 // n = (n+1) % KEY_BUFFER_SIZE
308 // (assuming positive values of n).
312 if ( n >= KEY_BUFFER_SIZE ) n=0;
316 // Returns 1 if character waiting... 0 otherwise
319 int is_one_waiting = 0;
321 if ( !key_inited ) return 0;
323 ENTER_CRITICAL_SECTION(&key_lock);
325 if (key_data.keytail != key_data.keyhead){
329 LEAVE_CRITICAL_SECTION(&key_lock);
331 return is_one_waiting;
334 // Return key scancode if a key has been pressed,
336 // Reads keys out of the key buffer and updates keyhead.
341 if ( !key_inited ) return 0;
343 ENTER_CRITICAL_SECTION(&key_lock);
345 if (key_data.keytail!=key_data.keyhead) {
346 key = key_data.keybuffer[key_data.keyhead];
347 key_data.keyhead = add_one(key_data.keyhead);
350 LEAVE_CRITICAL_SECTION(&key_lock);
355 // Unget a key. Puts it back in the input queue.
356 void key_outkey(int key)
360 if ( !key_inited ) return;
362 ENTER_CRITICAL_SECTION(&key_lock);
364 bufp = key_data.keytail+1;
366 if (bufp >= KEY_BUFFER_SIZE){
370 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
372 key_data.keytail = bufp;
374 LEAVE_CRITICAL_SECTION(&key_lock);
379 // Return amount of time last key was held down.
380 // This is currently (July 17, 1996) bogus because our timing is
382 int key_inkey_time(uint * time)
391 ENTER_CRITICAL_SECTION(&key_lock);
393 if (key_data.keytail!=key_data.keyhead) {
394 key = key_data.keybuffer[key_data.keyhead];
395 *time = key_data.time_pressed[key_data.keyhead];
396 key_data.keyhead = add_one(key_data.keyhead);
399 LEAVE_CRITICAL_SECTION(&key_lock);
405 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
406 // but does not update keyhead pointer.
411 if ( !key_inited ) return 0;
413 ENTER_CRITICAL_SECTION(&key_lock);
415 if (key_data.keytail!=key_data.keyhead) {
416 key = key_data.keybuffer[key_data.keyhead];
418 LEAVE_CRITICAL_SECTION(&key_lock);
423 // If not installed, uses BIOS and returns getch();
424 // Else returns pending key (or waits for one if none waiting).
430 if ( !key_inited ) return 0;
432 while (!key_checkch()){
442 // Set global shift_status with modifier results (shift, ctrl, alt).
443 uint key_get_shift_status()
445 unsigned int shift_status = 0;
447 if ( !key_inited ) return 0;
449 ENTER_CRITICAL_SECTION(&key_lock);
451 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
452 shift_status |= KEY_SHIFTED;
454 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
455 shift_status |= KEY_ALTED;
457 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
458 shift_status |= KEY_CTRLED;
461 if (keyd_pressed[KEY_DEBUG_KEY])
462 shift_status |= KEY_DEBUGGED;
464 if (keyd_pressed[KEY_DEBUG_KEY]) {
465 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
466 if ((Cheats_enabled) && Key_normal_game) {
467 mprintf(("Debug key\n"));
468 shift_status |= KEY_DEBUGGED1;
472 LEAVE_CRITICAL_SECTION(&key_lock);
477 // Returns amount of time key (specified by "code") has been down since last call.
478 // Returns float, unlike key_down_time() which returns a fix.
479 float key_down_timef(uint scancode)
481 uint time_down, time;
484 if ( !key_inited ) return 0.0f;
486 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
488 ENTER_CRITICAL_SECTION(&key_lock);
490 time = timer_get_milliseconds();
491 delta_time = time - key_data.TimeKeyDownChecked[scancode];
492 key_data.TimeKeyDownChecked[scancode] = time;
494 if ( delta_time <= 1 ) {
495 key_data.TimeKeyWentDown[scancode] = time;
496 if (keyd_pressed[scancode]) {
497 LEAVE_CRITICAL_SECTION(&key_lock);
500 LEAVE_CRITICAL_SECTION(&key_lock);
505 if ( !keyd_pressed[scancode] ) {
506 time_down = key_data.TimeKeyHeldDown[scancode];
507 key_data.TimeKeyHeldDown[scancode] = 0;
509 time_down = time - key_data.TimeKeyWentDown[scancode];
510 key_data.TimeKeyWentDown[scancode] = time;
513 LEAVE_CRITICAL_SECTION(&key_lock);
515 return i2fl(time_down) / i2fl(delta_time);
519 // Returns amount of time key (specified by "code") has been down since last call.
520 // Returns float, unlike key_down_time() which returns a fix.
521 fix key_down_time( uint code )
523 uint time_down, time;
526 if ( !key_inited ) return 0.0f;
528 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
530 EnterCriticalSection( &key_lock );
532 time = timer_get_milliseconds();
533 delta_time = time - TimeKeyDownChecked[scancode];
534 TimeKeyDownChecked[scancode] = time;
536 if ( delta_time <= 1 ) {
537 LeaveCriticalSection( &key_lock );
538 if (keyd_pressed[scancode])
544 if ( !keyd_pressed[scancode] ) {
545 time_down = key_data.TimeKeyHeldDown[scancode];
546 key_data.TimeKeyHeldDown[scancode] = 0;
548 time_down = time - key_data.TimeKeyWentDown[scancode];
549 key_data.TimeKeyWentDown[scancode] = time;
552 LeaveCriticalSection( &key_lock );
554 return fixmuldiv( time_down, F1_0, delta_time );
559 // Returns number of times key has went from up to down since last call.
560 int key_down_count(int scancode)
564 if ( !key_inited ) return 0;
565 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
567 ENTER_CRITICAL_SECTION(&key_lock);
569 n = key_data.NumDowns[scancode];
570 key_data.NumDowns[scancode] = 0;
572 LEAVE_CRITICAL_SECTION(&key_lock);
578 // Returns number of times key has went from down to up since last call.
579 int key_up_count(int scancode)
583 if ( !key_inited ) return 0;
584 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
586 ENTER_CRITICAL_SECTION(&key_lock);
588 n = key_data.NumUps[scancode];
589 key_data.NumUps[scancode] = 0;
591 LEAVE_CRITICAL_SECTION(&key_lock);
596 int key_check(int key)
598 return key_data.down_check[key];
601 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
602 // latency => time difference in ms between when key was actually pressed and now
603 //void key_mark( uint code, int state )
604 void key_mark( uint code, int state, uint latency )
606 uint scancode, breakbit, temp, event_time;
609 if ( !key_inited ) return;
611 ENTER_CRITICAL_SECTION(&key_lock);
613 // If running in the UK, need to translate their wacky slash scancode to ours
614 if ( code == KEY_SLASH_UK ) {
661 if ( (code == 0xc5) && !Key_running_NT ) {
662 key_turn_off_numlock();
665 Assert( code < NUM_KEYS );
667 event_time = timer_get_milliseconds() - latency;
668 // event_time = timeGetTime() - latency;
672 scancode = code & (NUM_KEYS-1);
680 keyd_last_released = scancode;
681 keyd_pressed[scancode] = 0;
682 key_data.NumUps[scancode]++;
684 // What is the point of this code? "temp" is never used!
686 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
687 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
688 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
690 temp |= keyd_pressed[KEY_DEBUG_KEY];
692 if (event_time < key_data.TimeKeyWentDown[scancode])
693 key_data.TimeKeyHeldDown[scancode] = 0;
695 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
698 keyd_last_pressed = scancode;
699 keyd_time_when_last_pressed = event_time;
700 if (!keyd_pressed[scancode]) {
702 key_data.TimeKeyWentDown[scancode] = event_time;
703 keyd_pressed[scancode] = 1;
704 key_data.NumDowns[scancode]++;
705 key_data.down_check[scancode]++;
707 // mprintf(( "Scancode = %x\n", scancode ));
709 // if ( scancode == KEY_BREAK )
713 } else if (!keyd_repeat) {
714 // Don't buffer repeating key if repeat mode is off
718 if ( scancode!=0xAA ) {
719 keycode = (unsigned short)scancode;
721 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
722 keycode |= KEY_SHIFTED;
724 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
725 keycode |= KEY_ALTED;
727 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
728 keycode |= KEY_CTRLED;
731 if ( keyd_pressed[KEY_DEBUG_KEY] )
732 keycode |= KEY_DEBUGGED;
733 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
735 // keyd_pressed[KEY_DEBUG_KEY] = 0;
736 // keyd_pressed[KEY_BACKSP] = 0;
740 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
741 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
742 if (Cheats_enabled && Key_normal_game) {
743 keycode |= KEY_DEBUGGED1;
750 temp = key_data.keytail+1;
751 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
753 if (temp!=key_data.keyhead) {
754 int i, accept_key = 1;
755 // Num_filter_keys will only be non-zero when a key filter has
756 // been explicity set up via key_set_filter()
757 for ( i = 0; i < Num_filter_keys; i++ ) {
759 if ( Key_filter[i] == keycode ) {
766 key_data.keybuffer[key_data.keytail] = keycode;
767 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
768 key_data.keytail = temp;
775 LEAVE_CRITICAL_SECTION(&key_lock);
778 #ifdef USE_DIRECTINPUT
786 if ( !key_inited ) return;
788 #ifdef USE_DIRECTINPUT
792 if ( Key_numlock_was_on ) {
793 key_turn_on_numlock();
794 Key_numlock_was_on = 0;
801 DeleteCriticalSection( &key_lock );
808 if ( key_inited ) return;
814 InitializeCriticalSection( &key_lock );
816 ENTER_CRITICAL_SECTION(&key_lock);
819 keyd_time_when_last_pressed = timer_get_milliseconds();
820 keyd_buffer_type = 1;
823 // Clear the keyboard array
832 LEAVE_CRITICAL_SECTION(&key_lock);
834 #ifdef USE_DIRECTINPUT
839 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
841 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
845 if ( key_numlock_is_on() ) {
846 Key_numlock_was_on = 1;
847 key_turn_off_numlock();
855 void key_level_init()
859 for (i=0; i<NUM_KEYS; i++)
860 key_data.down_check[i] = 0;
863 void key_lost_focus()
865 if ( !key_inited ) return;
872 if ( !key_inited ) return;
877 // Restricts the keys that are accepted from the keyboard
879 // filter_array => array of keys to act as a filter
880 // num => number of keys in filter_array
882 void key_set_filter(int *filter_array, int num)
886 if ( num >= MAX_FILTER_KEYS ) {
888 num = MAX_FILTER_KEYS;
891 Num_filter_keys = num;
893 for ( i = 0; i < num; i++ ) {
894 Key_filter[i] = filter_array[i];
898 // Clear the key filter, so all keypresses are accepted from keyboard
900 void key_clear_filter()
905 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
911 #ifdef USE_DIRECTINPUT
913 // JAS - April 18, 1998
914 // Not using because DI has the following problems: (Everything else works ok)
915 // Under NT, Pause and Numlock report as identical keys.
916 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
920 //============================================================================
922 // For the keyboard, this basically replaces our old functionallity of:
927 //============================================================================
932 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
934 static LPDIRECTINPUT Di_object = NULL;
935 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
936 static HANDLE Di_thread = NULL;
937 static DWORD Di_thread_id = NULL;
938 static HANDLE Di_event = NULL;
940 DWORD di_process(DWORD lparam)
943 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
945 //mprintf(( "Got event!\n" ));
949 DIDEVICEOBJECTDATA rgdod[10];
950 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
953 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
955 if (hr == DIERR_INPUTLOST) {
957 * DirectInput is telling us that the input stream has
958 * been interrupted. We aren't tracking any state
959 * between polls, so we don't have any special reset
960 * that needs to be done. We just re-acquire and
963 Sleep(1000); // Pause a second...
964 hr = Di_keyboard->Acquire();
971 // dwItems = number of elements read (could be zero)
972 if (hr == DI_BUFFEROVERFLOW) {
973 // Buffer had overflowed.
974 mprintf(( "Buffer overflowed!\n" ));
978 //mprintf(( "Got %d events\n", dwItems ));
980 for (i=0; i<(int)dwItems; i++ ) {
981 int key = rgdod[i].dwOfs;
982 int state = rgdod[i].dwData;
983 int stamp = rgdod[i].dwTimeStamp;
986 latency = timeGetTime() - stamp;
990 // if ( key == KEY_PRINT_SCRN ) {
991 // key_mark( key, 1, latency );
993 // key_mark( key, (state&0x80?1:0), latency );
994 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1014 * Register with the DirectInput subsystem and get a pointer
1015 * to a IDirectInput interface we can use.
1021 * Instance handle to our application or DLL.
1023 * DIRECTINPUT_VERSION
1025 * The version of DirectInput we were designed for.
1026 * We take the value from the <dinput.h> header file.
1030 * Receives pointer to the IDirectInput interface
1035 * We do not use OLE aggregation, so this parameter
1039 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1042 mprintf(( "DirectInputCreate failed!\n" ));
1047 * Obtain an interface to the system keyboard device.
1053 * The instance GUID for the device we wish to access.
1054 * GUID_SysKeyboard is a predefined instance GUID that
1055 * always refers to the system keyboard device.
1059 * Receives pointer to the IDirectInputDevice interface
1064 * We do not use OLE aggregation, so this parameter
1068 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1071 mprintf(( "CreateDevice failed!\n" ));
1076 * Set the data format to "keyboard format".
1078 * A data format specifies which controls on a device we
1079 * are interested in, and how they should be reported.
1081 * This tells DirectInput that we will be passing an array
1082 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1088 * Predefined data format which describes
1089 * an array of 256 bytes, one per scancode.
1091 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1094 mprintf(( "SetDataFormat failed!\n" ));
1100 * Set the cooperativity level to let DirectInput know how
1101 * this device should interact with the system and with other
1102 * DirectInput applications.
1106 * DISCL_NONEXCLUSIVE
1108 * Retrieve keyboard data when acquired, not interfering
1109 * with any other applications which are reading keyboard
1114 * If the user switches away from our application,
1115 * automatically release the keyboard back to the system.
1118 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1121 mprintf(( "SetCooperativeLevel failed!\n" ));
1127 // Turn on buffering
1128 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1129 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1131 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1132 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1134 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1136 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1141 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1142 Assert(Di_event != NULL);
1144 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1145 Assert( Di_thread != NULL );
1147 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1149 hr = Di_keyboard->SetEventNotification(Di_event);
1151 mprintf(( "SetEventNotification failed\n" ));
1155 Di_keyboard->Acquire();
1163 * Destroy any lingering IDirectInputDevice object.
1168 * Cleanliness is next to godliness. Unacquire the device
1169 * one last time just in case we got really confused and tried
1170 * to exit while the device is still acquired.
1172 Di_keyboard->Unacquire();
1174 Di_keyboard->Release();
1179 * Destroy any lingering IDirectInput object.
1182 Di_object->Release();
1187 CloseHandle(Di_event);