2 * $Logfile: /Freespace2/code/Io/Key.cpp $
7 * <insert description of file here>
10 * Revision 1.1 2002/05/03 03:28:09 root
14 * 6 10/29/99 6:10p Jefff
15 * squashed the damned y/z german issues once and for all
17 * 5 6/07/99 1:21p Dave
18 * Fixed debug console scrolling problem. Thread related.
20 * 4 6/02/99 6:18p Dave
21 * Fixed TNT lockup problems! Wheeeee!
23 * 3 11/05/98 4:18p Dave
24 * First run nebula support. Beefed up localization a bit. Removed all
25 * conditional compiles for foreign versions. Modified mission file
28 * 2 10/07/98 10:53a Dave
31 * 1 10/07/98 10:49a Dave
33 * 37 6/19/98 3:50p Lawrance
34 * change GERMAN to GR_BUILD
36 * 36 6/17/98 11:05a Lawrance
37 * translate french and german keys
39 * 35 6/12/98 4:49p Hoffoss
40 * Added code to remap scancodes for german and french keyboards.
42 * 34 5/20/98 12:10a Mike
45 * 33 5/19/98 12:19p Mike
48 * 32 5/19/98 12:28a Mike
51 * 31 5/18/98 11:01p Mike
52 * Adding support for cheat system.
54 * 30 5/11/98 12:09a Lawrance
55 * Put in code to turn on/off NumLock key when running under 95
57 * 29 5/01/98 4:23p Lawrance
58 * Remap the scancode for the UK "\" key
60 * 28 4/18/98 12:42p John
61 * Added code to use DirectInput to read keyboard. Took out because it
62 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
64 * 27 4/13/98 10:16a John
65 * Switched gettime back to timer_get_milliseconds, which is now thread
68 * 26 4/12/98 11:08p Lawrance
69 * switch back to using gettime() in separate threads
71 * 25 4/12/98 5:31p Lawrance
72 * use timer_get_milliseconds() instead of gettime()
74 * 24 3/25/98 8:08p John
75 * Restructured software rendering into two modules; One for windowed
76 * debug mode and one for DirectX fullscreen.
78 * 23 1/23/98 3:49p Mike
79 * Fix bug in negative time-down due to latency.
81 * 22 1/07/98 6:41p Lawrance
82 * Pass message latency to the keyboard lib.
84 * 21 11/17/97 10:42a John
85 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
86 * happened, so they're not stuck down.
88 * 20 11/14/97 4:33p Mike
89 * Change Debug key to backquote (from F11).
90 * Balance a ton of subsystems in ships.tbl.
91 * Change "Heavy Laser" to "Disruptor".
93 * 19 9/13/97 9:30a Lawrance
94 * added ability to block certain keys from the keyboard
96 * 18 9/10/97 6:02p Hoffoss
97 * Added code to check for key-pressed sexp operator in FreeSpace as part
98 * of training mission stuff.
100 * 17 9/09/97 11:08a Sandeep
101 * fixed warning level 4
103 * 16 7/29/97 5:30p Lawrance
104 * move gettime() to timer module
106 * 15 4/22/97 10:56a John
107 * fixed some resource leaks.
109 * 14 2/03/97 4:23p Allender
110 * use F11 as debug key now
112 * 13 1/10/97 5:15p Mike
113 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
115 * Added turret code to AI system.
120 //#define USE_DIRECTINPUT
124 #include <windowsx.h>
132 #include "localize.h"
134 #define KEY_BUFFER_SIZE 16
136 //-------- Variable accessed by outside functions ---------
137 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
139 uint keyd_last_pressed;
140 uint keyd_last_released;
141 ubyte keyd_pressed[NUM_KEYS];
142 int keyd_time_when_last_pressed;
144 typedef struct keyboard {
145 ushort keybuffer[KEY_BUFFER_SIZE];
146 uint time_pressed[KEY_BUFFER_SIZE];
147 uint TimeKeyWentDown[NUM_KEYS];
148 uint TimeKeyHeldDown[NUM_KEYS];
149 uint TimeKeyDownChecked[NUM_KEYS];
150 uint NumDowns[NUM_KEYS];
151 uint NumUps[NUM_KEYS];
152 int down_check[NUM_KEYS]; // nonzero if has been pressed yet this mission
153 uint keyhead, keytail;
160 CRITICAL_SECTION key_lock;
162 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
163 // This flag was created since the backspace key is also used to correct mistakes
164 // when typing in your pilots callsign. This global flag is checked before execution
167 int ascii_table[128] =
168 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
169 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
170 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
171 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
172 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
173 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
174 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
175 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
176 255,255,255,255,255,255,255,255 };
178 int shifted_ascii_table[128] =
179 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
180 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
181 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
182 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
183 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
184 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
185 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
186 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
187 255,255,255,255,255,255,255,255 };
189 // used to limit the keypresses that are accepted from the keyboard
190 #define MAX_FILTER_KEYS 64
192 int Key_filter[MAX_FILTER_KEYS];
194 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
195 static int Key_running_NT = 0; // NT is the OS
197 int Cheats_enabled = 0;
198 int Key_normal_game = 0;
200 int key_numlock_is_on()
204 SDL_GetKeyState(keys);
205 if ( keys[SDLK_NUMLOCK] ) {
209 unsigned char keys[256];
210 GetKeyboardState(keys);
211 if ( keys[VK_NUMLOCK] ) {
218 void key_turn_off_numlock()
223 unsigned char keys[256];
224 GetKeyboardState(keys);
225 keys[VK_NUMLOCK] = 0;
226 SetKeyboardState(keys);
230 void key_turn_on_numlock()
235 unsigned char keys[256];
236 GetKeyboardState(keys);
237 keys[VK_NUMLOCK] = 1;
238 SetKeyboardState(keys);
242 // Convert a BIOS scancode to ASCII.
243 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
244 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
245 int key_to_ascii(int keycode )
249 if ( !key_inited ) return 255;
251 shifted = keycode & KEY_SHIFTED;
258 return shifted_ascii_table[keycode];
260 return ascii_table[keycode];
263 // Flush the keyboard buffer.
264 // Clear the keyboard array (keyd_pressed).
270 if ( !key_inited ) return;
275 ENTER_CRITICAL_SECTION(&key_lock);
277 key_data.keyhead = key_data.keytail = 0;
279 //Clear the keyboard buffer
280 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
281 key_data.keybuffer[i] = 0;
282 key_data.time_pressed[i] = 0;
285 //Clear the keyboard array
287 CurTime = timer_get_milliseconds();
290 for (i=0; i<NUM_KEYS; i++ ) {
292 key_data.TimeKeyDownChecked[i] = CurTime;
293 key_data.TimeKeyWentDown[i] = CurTime;
294 key_data.TimeKeyHeldDown[i] = 0;
295 key_data.NumDowns[i]=0;
296 key_data.NumUps[i]=0;
299 LEAVE_CRITICAL_SECTION(&key_lock);
303 // A nifty function which performs the function:
304 // n = (n+1) % KEY_BUFFER_SIZE
305 // (assuming positive values of n).
309 if ( n >= KEY_BUFFER_SIZE ) n=0;
313 // Returns 1 if character waiting... 0 otherwise
316 int is_one_waiting = 0;
318 if ( !key_inited ) return 0;
320 ENTER_CRITICAL_SECTION(&key_lock);
322 if (key_data.keytail != key_data.keyhead){
326 LEAVE_CRITICAL_SECTION(&key_lock);
328 return is_one_waiting;
331 // Return key scancode if a key has been pressed,
333 // Reads keys out of the key buffer and updates keyhead.
338 if ( !key_inited ) return 0;
340 ENTER_CRITICAL_SECTION(&key_lock);
342 if (key_data.keytail!=key_data.keyhead) {
343 key = key_data.keybuffer[key_data.keyhead];
344 key_data.keyhead = add_one(key_data.keyhead);
347 LEAVE_CRITICAL_SECTION(&key_lock);
352 // Unget a key. Puts it back in the input queue.
353 void key_outkey(int key)
357 if ( !key_inited ) return;
359 ENTER_CRITICAL_SECTION(&key_lock);
361 bufp = key_data.keytail+1;
363 if (bufp >= KEY_BUFFER_SIZE){
367 key_data.keybuffer[key_data.keytail] = (unsigned short)key;
369 key_data.keytail = bufp;
371 LEAVE_CRITICAL_SECTION(&key_lock);
376 // Return amount of time last key was held down.
377 // This is currently (July 17, 1996) bogus because our timing is
379 int key_inkey_time(uint * time)
388 ENTER_CRITICAL_SECTION(&key_lock);
390 if (key_data.keytail!=key_data.keyhead) {
391 key = key_data.keybuffer[key_data.keyhead];
392 *time = key_data.time_pressed[key_data.keyhead];
393 key_data.keyhead = add_one(key_data.keyhead);
396 LEAVE_CRITICAL_SECTION(&key_lock);
402 // Returns scancode of last key pressed, if any (returns 0 if no key pressed)
403 // but does not update keyhead pointer.
408 if ( !key_inited ) return 0;
410 ENTER_CRITICAL_SECTION(&key_lock);
412 if (key_data.keytail!=key_data.keyhead) {
413 key = key_data.keybuffer[key_data.keyhead];
415 LEAVE_CRITICAL_SECTION(&key_lock);
420 // If not installed, uses BIOS and returns getch();
421 // Else returns pending key (or waits for one if none waiting).
427 if ( !key_inited ) return 0;
429 while (!key_checkch()){
439 // Set global shift_status with modifier results (shift, ctrl, alt).
440 uint key_get_shift_status()
442 unsigned int shift_status = 0;
444 if ( !key_inited ) return 0;
446 ENTER_CRITICAL_SECTION(&key_lock);
448 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
449 shift_status |= KEY_SHIFTED;
451 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
452 shift_status |= KEY_ALTED;
454 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
455 shift_status |= KEY_CTRLED;
458 if (keyd_pressed[KEY_DEBUG_KEY])
459 shift_status |= KEY_DEBUGGED;
461 if (keyd_pressed[KEY_DEBUG_KEY]) {
462 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
463 if ((Cheats_enabled) && Key_normal_game) {
464 mprintf(("Debug key\n"));
465 shift_status |= KEY_DEBUGGED1;
469 LEAVE_CRITICAL_SECTION(&key_lock);
474 // Returns amount of time key (specified by "code") has been down since last call.
475 // Returns float, unlike key_down_time() which returns a fix.
476 float key_down_timef(uint scancode)
478 uint time_down, time;
481 if ( !key_inited ) return 0.0f;
483 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
485 ENTER_CRITICAL_SECTION(&key_lock);
487 time = timer_get_milliseconds();
488 delta_time = time - key_data.TimeKeyDownChecked[scancode];
489 key_data.TimeKeyDownChecked[scancode] = time;
491 if ( delta_time <= 1 ) {
492 key_data.TimeKeyWentDown[scancode] = time;
493 if (keyd_pressed[scancode]) {
494 LEAVE_CRITICAL_SECTION(&key_lock);
497 LEAVE_CRITICAL_SECTION(&key_lock);
502 if ( !keyd_pressed[scancode] ) {
503 time_down = key_data.TimeKeyHeldDown[scancode];
504 key_data.TimeKeyHeldDown[scancode] = 0;
506 time_down = time - key_data.TimeKeyWentDown[scancode];
507 key_data.TimeKeyWentDown[scancode] = time;
510 LEAVE_CRITICAL_SECTION(&key_lock);
512 return i2fl(time_down) / i2fl(delta_time);
516 // Returns amount of time key (specified by "code") has been down since last call.
517 // Returns float, unlike key_down_time() which returns a fix.
518 fix key_down_time( uint code )
520 uint time_down, time;
523 if ( !key_inited ) return 0.0f;
525 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0.0f;
527 EnterCriticalSection( &key_lock );
529 time = timer_get_milliseconds();
530 delta_time = time - TimeKeyDownChecked[scancode];
531 TimeKeyDownChecked[scancode] = time;
533 if ( delta_time <= 1 ) {
534 LeaveCriticalSection( &key_lock );
535 if (keyd_pressed[scancode])
541 if ( !keyd_pressed[scancode] ) {
542 time_down = key_data.TimeKeyHeldDown[scancode];
543 key_data.TimeKeyHeldDown[scancode] = 0;
545 time_down = time - key_data.TimeKeyWentDown[scancode];
546 key_data.TimeKeyWentDown[scancode] = time;
549 LeaveCriticalSection( &key_lock );
551 return fixmuldiv( time_down, F1_0, delta_time );
556 // Returns number of times key has went from up to down since last call.
557 int key_down_count(int scancode)
561 if ( !key_inited ) return 0;
562 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
564 ENTER_CRITICAL_SECTION(&key_lock);
566 n = key_data.NumDowns[scancode];
567 key_data.NumDowns[scancode] = 0;
569 LEAVE_CRITICAL_SECTION(&key_lock);
575 // Returns number of times key has went from down to up since last call.
576 int key_up_count(int scancode)
580 if ( !key_inited ) return 0;
581 if ((scancode<0)|| (scancode>=NUM_KEYS)) return 0;
583 ENTER_CRITICAL_SECTION(&key_lock);
585 n = key_data.NumUps[scancode];
586 key_data.NumUps[scancode] = 0;
588 LEAVE_CRITICAL_SECTION(&key_lock);
593 int key_check(int key)
595 return key_data.down_check[key];
598 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
599 // latency => time difference in ms between when key was actually pressed and now
600 //void key_mark( uint code, int state )
601 void key_mark( uint code, int state, uint latency )
603 uint scancode, breakbit, temp, event_time;
606 if ( !key_inited ) return;
608 ENTER_CRITICAL_SECTION(&key_lock);
610 // If running in the UK, need to translate their wacky slash scancode to ours
611 if ( code == KEY_SLASH_UK ) {
658 if ( (code == 0xc5) && !Key_running_NT ) {
659 key_turn_off_numlock();
662 Assert( code < NUM_KEYS );
664 event_time = timer_get_milliseconds() - latency;
665 // event_time = timeGetTime() - latency;
668 scancode = code & (NUM_KEYS-1);
673 keyd_last_released = scancode;
674 keyd_pressed[scancode] = 0;
675 key_data.NumUps[scancode]++;
677 // What is the point of this code? "temp" is never used!
679 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
680 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
681 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
683 temp |= keyd_pressed[KEY_DEBUG_KEY];
685 if (event_time < key_data.TimeKeyWentDown[scancode])
686 key_data.TimeKeyHeldDown[scancode] = 0;
688 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
691 keyd_last_pressed = scancode;
692 keyd_time_when_last_pressed = event_time;
693 if (!keyd_pressed[scancode]) {
695 key_data.TimeKeyWentDown[scancode] = event_time;
696 keyd_pressed[scancode] = 1;
697 key_data.NumDowns[scancode]++;
698 key_data.down_check[scancode]++;
700 // mprintf(( "Scancode = %x\n", scancode ));
702 // if ( scancode == KEY_BREAK )
706 } else if (!keyd_repeat) {
707 // Don't buffer repeating key if repeat mode is off
711 if ( scancode!=0xAA ) {
712 keycode = (unsigned short)scancode;
714 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
715 keycode |= KEY_SHIFTED;
717 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
718 keycode |= KEY_ALTED;
720 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
721 keycode |= KEY_CTRLED;
724 if ( keyd_pressed[KEY_DEBUG_KEY] )
725 keycode |= KEY_DEBUGGED;
726 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
728 // keyd_pressed[KEY_DEBUG_KEY] = 0;
729 // keyd_pressed[KEY_BACKSP] = 0;
733 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
734 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
735 if (Cheats_enabled && Key_normal_game) {
736 keycode |= KEY_DEBUGGED1;
743 temp = key_data.keytail+1;
744 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
746 if (temp!=key_data.keyhead) {
747 int i, accept_key = 1;
748 // Num_filter_keys will only be non-zero when a key filter has
749 // been explicity set up via key_set_filter()
750 for ( i = 0; i < Num_filter_keys; i++ ) {
752 if ( Key_filter[i] == keycode ) {
759 key_data.keybuffer[key_data.keytail] = keycode;
760 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
761 key_data.keytail = temp;
768 LEAVE_CRITICAL_SECTION(&key_lock);
771 #ifdef USE_DIRECTINPUT
779 if ( !key_inited ) return;
781 #ifdef USE_DIRECTINPUT
785 if ( Key_numlock_was_on ) {
786 key_turn_on_numlock();
787 Key_numlock_was_on = 0;
794 DeleteCriticalSection( &key_lock );
801 if ( key_inited ) return;
807 InitializeCriticalSection( &key_lock );
809 ENTER_CRITICAL_SECTION(&key_lock);
812 keyd_time_when_last_pressed = timer_get_milliseconds();
813 keyd_buffer_type = 1;
816 // Clear the keyboard array
825 LEAVE_CRITICAL_SECTION(&key_lock);
827 #ifdef USE_DIRECTINPUT
832 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
834 if ( ver.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
838 if ( key_numlock_is_on() ) {
839 Key_numlock_was_on = 1;
840 key_turn_off_numlock();
848 void key_level_init()
852 for (i=0; i<NUM_KEYS; i++)
853 key_data.down_check[i] = 0;
856 void key_lost_focus()
858 if ( !key_inited ) return;
865 if ( !key_inited ) return;
870 // Restricts the keys that are accepted from the keyboard
872 // filter_array => array of keys to act as a filter
873 // num => number of keys in filter_array
875 void key_set_filter(int *filter_array, int num)
879 if ( num >= MAX_FILTER_KEYS ) {
881 num = MAX_FILTER_KEYS;
884 Num_filter_keys = num;
886 for ( i = 0; i < num; i++ ) {
887 Key_filter[i] = filter_array[i];
891 // Clear the key filter, so all keypresses are accepted from keyboard
893 void key_clear_filter()
898 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {
904 #ifdef USE_DIRECTINPUT
906 // JAS - April 18, 1998
907 // Not using because DI has the following problems: (Everything else works ok)
908 // Under NT, Pause and Numlock report as identical keys.
909 // Under 95, Pause is the same as pressing Ctrl then Numlock. So the game fires each
913 //============================================================================
915 // For the keyboard, this basically replaces our old functionallity of:
920 //============================================================================
925 #define MAX_BUFFERED_KEYBOARD_EVENTS 10
927 static LPDIRECTINPUT Di_object = NULL;
928 static LPDIRECTINPUTDEVICE Di_keyboard = NULL;
929 static HANDLE Di_thread = NULL;
930 static DWORD Di_thread_id = NULL;
931 static HANDLE Di_event = NULL;
933 DWORD di_process(DWORD lparam)
936 if ( WaitForSingleObject( Di_event, INFINITE )==WAIT_OBJECT_0 ) {
938 //mprintf(( "Got event!\n" ));
942 DIDEVICEOBJECTDATA rgdod[10];
943 DWORD dwItems = MAX_BUFFERED_KEYBOARD_EVENTS;
946 hr = Di_keyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), rgdod, &dwItems, 0);
948 if (hr == DIERR_INPUTLOST) {
950 * DirectInput is telling us that the input stream has
951 * been interrupted. We aren't tracking any state
952 * between polls, so we don't have any special reset
953 * that needs to be done. We just re-acquire and
956 Sleep(1000); // Pause a second...
957 hr = Di_keyboard->Acquire();
964 // dwItems = number of elements read (could be zero)
965 if (hr == DI_BUFFEROVERFLOW) {
966 // Buffer had overflowed.
967 mprintf(( "Buffer overflowed!\n" ));
971 //mprintf(( "Got %d events\n", dwItems ));
973 for (i=0; i<(int)dwItems; i++ ) {
974 int key = rgdod[i].dwOfs;
975 int state = rgdod[i].dwData;
976 int stamp = rgdod[i].dwTimeStamp;
979 latency = timeGetTime() - stamp;
983 // if ( key == KEY_PRINT_SCRN ) {
984 // key_mark( key, 1, latency );
986 // key_mark( key, (state&0x80?1:0), latency );
987 mprintf(( "Key=%x, State=%x, Time=%d, Latency=%d\n", key, state, stamp, latency ));
1007 * Register with the DirectInput subsystem and get a pointer
1008 * to a IDirectInput interface we can use.
1014 * Instance handle to our application or DLL.
1016 * DIRECTINPUT_VERSION
1018 * The version of DirectInput we were designed for.
1019 * We take the value from the <dinput.h> header file.
1023 * Receives pointer to the IDirectInput interface
1028 * We do not use OLE aggregation, so this parameter
1032 hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL);
1035 mprintf(( "DirectInputCreate failed!\n" ));
1040 * Obtain an interface to the system keyboard device.
1046 * The instance GUID for the device we wish to access.
1047 * GUID_SysKeyboard is a predefined instance GUID that
1048 * always refers to the system keyboard device.
1052 * Receives pointer to the IDirectInputDevice interface
1057 * We do not use OLE aggregation, so this parameter
1061 hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL);
1064 mprintf(( "CreateDevice failed!\n" ));
1069 * Set the data format to "keyboard format".
1071 * A data format specifies which controls on a device we
1072 * are interested in, and how they should be reported.
1074 * This tells DirectInput that we will be passing an array
1075 * of 256 bytes to IDirectInputDevice::GetDeviceState.
1081 * Predefined data format which describes
1082 * an array of 256 bytes, one per scancode.
1084 hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard);
1087 mprintf(( "SetDataFormat failed!\n" ));
1093 * Set the cooperativity level to let DirectInput know how
1094 * this device should interact with the system and with other
1095 * DirectInput applications.
1099 * DISCL_NONEXCLUSIVE
1101 * Retrieve keyboard data when acquired, not interfering
1102 * with any other applications which are reading keyboard
1107 * If the user switches away from our application,
1108 * automatically release the keyboard back to the system.
1111 hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
1114 mprintf(( "SetCooperativeLevel failed!\n" ));
1120 // Turn on buffering
1121 hdr.diph.dwSize = sizeof(DIPROPDWORD);
1122 hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1124 hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device
1125 hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS;
1127 hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph );
1129 mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" ));
1134 Di_event = CreateEvent( NULL, FALSE, FALSE, NULL );
1135 Assert(Di_event != NULL);
1137 Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id);
1138 Assert( Di_thread != NULL );
1140 SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST);
1142 hr = Di_keyboard->SetEventNotification(Di_event);
1144 mprintf(( "SetEventNotification failed\n" ));
1148 Di_keyboard->Acquire();
1156 * Destroy any lingering IDirectInputDevice object.
1161 * Cleanliness is next to godliness. Unacquire the device
1162 * one last time just in case we got really confused and tried
1163 * to exit while the device is still acquired.
1165 Di_keyboard->Unacquire();
1167 Di_keyboard->Release();
1172 * Destroy any lingering IDirectInput object.
1175 Di_object->Release();
1180 CloseHandle(Di_event);