2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: key.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
20 #define WIN32_LEAN_AND_MEAN
34 #define KEY_BUFFER_SIZE 16
36 //-------- Variable accessed by outside functions ---------
37 unsigned char keyd_print_screen=0;
39 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
40 unsigned char keyd_repeat;
41 unsigned char keyd_editor_mode;
42 volatile unsigned char keyd_last_pressed;
43 volatile unsigned char keyd_last_released;
44 volatile unsigned char keyd_pressed[256];
45 volatile int keyd_time_when_last_pressed;
47 typedef struct keyboard {
48 unsigned short keybuffer[KEY_BUFFER_SIZE];
49 fix time_pressed[KEY_BUFFER_SIZE];
50 fix TimeKeyWentDown[256];
51 fix TimeKeyHeldDown[256];
52 unsigned int NumDowns[256];
53 unsigned int NumUps[256];
54 unsigned int keyhead, keytail;
59 static volatile keyboard key_data;
61 static unsigned char Installed=0;
63 unsigned char ascii_table[128] =
64 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
65 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
66 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
67 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
68 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
69 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
70 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
71 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
72 255,255,255,255,255,255,255,255 };
74 unsigned char shifted_ascii_table[128] =
75 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
76 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
77 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
78 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
79 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
80 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
81 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
82 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
83 255,255,255,255,255,255,255,255 };
86 // Initialization and Cleanup Routines
90 keyd_time_when_last_pressed = timer_get_fixed_seconds();
96 // Clear the keyboard array
98 if (!Installed) Installed = 1;
104 if (!Installed) return;
105 key_clear_bios_buffer_all();
109 extern char key_to_ascii(int keycode )
113 shifted = keycode & KEY_SHIFTED;
120 return shifted_ascii_table[keycode];
122 return ascii_table[keycode];
125 void key_clear_bios_buffer_all()
130 void key_clear_bios_buffer()
140 // Clear the BIOS buffer
141 key_clear_bios_buffer();
143 key_data.keyhead = key_data.keytail = 0;
145 //Clear the keyboard buffer
146 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
147 key_data.keybuffer[i] = 0;
148 key_data.time_pressed[i] = 0;
151 //Clear the keyboard array
153 CurTime =timer_get_fixed_secondsX();
155 for (i=0; i<256; i++ ) {
157 key_data.TimeKeyWentDown[i] = CurTime;
158 key_data.TimeKeyHeldDown[i] = 0;
159 key_data.NumDowns[i]=0;
160 key_data.NumUps[i]=0;
162 keyd_print_screen = 0;
168 if ( n >= KEY_BUFFER_SIZE ) n=0;
172 // Returns 1 if character waiting... 0 otherwise
175 int is_one_waiting = 0;
177 key_clear_bios_buffer();
179 if (key_data.keytail!=key_data.keyhead)
182 return is_one_waiting;
189 key_clear_bios_buffer();
191 if (key_data.keytail!=key_data.keyhead) {
192 key = key_data.keybuffer[key_data.keyhead];
193 key_data.keyhead = add_one(key_data.keyhead);
199 int key_inkey_time(fix * time)
203 key_clear_bios_buffer();
205 if (key_data.keytail!=key_data.keyhead) {
206 key = key_data.keybuffer[key_data.keyhead];
207 *time = key_data.time_pressed[key_data.keyhead];
208 key_data.keyhead = add_one(key_data.keyhead);
215 void key_putkey (unsigned short keycode)
217 // this function simulates a key stroke entered
221 temp = key_data.keytail+1;
222 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
223 if (temp!=key_data.keyhead)
225 key_data.keybuffer[key_data.keytail] = keycode;
226 key_data.keytail = temp;
235 key_clear_bios_buffer();
237 if (key_data.keytail!=key_data.keyhead) {
238 key = key_data.keybuffer[key_data.keyhead];
244 // If not installed, uses BIOS and returns getch();
245 // Else returns pending key (or waits for one if none waiting).
253 while (!key_checkch())
258 unsigned int key_get_shift_status()
260 unsigned int shift_status = 0;
263 key_clear_bios_buffer();
265 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
266 shift_status |= KEY_SHIFTED;
268 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
269 shift_status |= KEY_ALTED;
271 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
272 shift_status |= KEY_CTRLED;
275 if (keyd_pressed[KEY_DELETE])
276 shift_status |=KEY_DEBUGGED;
283 // Returns the number of seconds this key has been down since last call.
284 fix key_down_time(int scancode) {
287 if ((scancode<0)|| (scancode>255)) return 0;
290 if (keyd_editor_mode && key_get_shift_status() )
294 if ( !keyd_pressed[scancode] ) {
295 time_down = key_data.TimeKeyHeldDown[scancode];
296 key_data.TimeKeyHeldDown[scancode] = 0;
298 time = timer_get_fixed_secondsX();
299 time_down = time - key_data.TimeKeyWentDown[scancode];
300 key_data.TimeKeyWentDown[scancode] = time;
306 // Returns number of times key has went from up to down since last call.
307 unsigned int key_down_count(int scancode) {
310 if ((scancode<0)|| (scancode>255)) return 0;
312 n = key_data.NumDowns[scancode];
313 key_data.NumDowns[scancode] = 0;
319 // Returns number of times key has went from down to up since last call.
320 unsigned int key_up_count(int scancode) {
323 if ((scancode<0)|| (scancode>255)) return 0;
325 n = key_data.NumUps[scancode];
326 key_data.NumUps[scancode] = 0;
331 // Use intrinsic forms so that we stay in the locked interrup code.
334 #pragma aux Int5 = "int 5";
338 // Send keyboard message to Descent keyboard system.
340 void send_key_msg(UINT msg, WPARAM vkeycode, LPARAM keypack)
342 unsigned char scancode, temp;
343 unsigned short keycode;
345 // Extract 8-bit OEM scancode
346 scancode = (char)((keypack >> 16) & 0xff);
348 if (vkeycode == VK_SNAPSHOT) {
349 scancode = KEY_PRINT_SCREEN;
350 keyd_print_screen = 1;
352 else if (keypack & 0x01000000) scancode |= 0x80;
353 if (vkeycode == VK_PAUSE) scancode = KEY_PAUSE;
358 case WM_KEYDOWN: // Keyboard down
359 // Handle Pauses the easy way.
360 keyd_last_pressed = scancode;
361 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
363 if (!keyd_pressed[scancode]) {
364 // First time key is down.
365 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
366 keyd_pressed[scancode] = 1;
367 key_data.NumDowns[scancode]++;
370 if ((keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP)) {
371 keyd_pressed[KEY_LSHIFT] = 0;
376 else if (!keyd_repeat) {
377 // don't buffer repeating key if repeat mode is off
380 if (scancode != 0xaa) {
381 keycode = (unsigned short)scancode;
383 if (keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
384 keycode |= KEY_SHIFTED;
386 if (keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
387 keycode |= KEY_ALTED;
389 if (keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
390 keycode |= KEY_CTRLED;
393 if (keyd_pressed[KEY_DELETE])
394 keycode |= KEY_DEBUGGED;
397 temp = key_data.keytail+1;
398 if (temp >= KEY_BUFFER_SIZE) temp = 0;
400 if (temp!= key_data.keyhead) {
401 key_data.keybuffer[key_data.keytail] = keycode;
402 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
403 key_data.keytail = temp;
409 case WM_KEYUP: // handle key ups
410 keyd_last_released = scancode;
411 keyd_pressed[scancode] = 0;
412 key_data.NumUps[scancode]++;
414 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
415 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
416 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
419 temp |= keyd_pressed[KEY_DELETE];
420 if (!(keyd_editor_mode && temp))
422 // NOTICE LINK TO ABOVE IF!!!!
423 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() -
424 key_data.TimeKeyWentDown[scancode];