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.
14 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
29 #define KEY_BUFFER_SIZE 16
31 //-------- Variable accessed by outside functions ---------
32 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
33 unsigned char keyd_repeat;
34 unsigned char keyd_editor_mode;
35 volatile unsigned char keyd_last_pressed;
36 volatile unsigned char keyd_last_released;
37 volatile unsigned char keyd_pressed[256];
38 volatile int keyd_time_when_last_pressed;
40 typedef struct keyboard {
41 unsigned short keybuffer[KEY_BUFFER_SIZE];
42 fix time_pressed[KEY_BUFFER_SIZE];
43 fix TimeKeyWentDown[256];
44 fix TimeKeyHeldDown[256];
45 unsigned int NumDowns[256];
46 unsigned int NumUps[256];
47 unsigned int keyhead, keytail;
51 void (__interrupt __far *prev_int_9)();
54 static volatile keyboard key_data;
56 static unsigned char Installed=0;
58 unsigned char ascii_table[128] =
59 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
60 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
61 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
62 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
63 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
64 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
65 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
66 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
67 255,255,255,255,255,255,255,255 };
69 unsigned char shifted_ascii_table[128] =
70 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
71 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
72 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
73 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
74 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
75 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
76 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
77 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
78 255,255,255,255,255,255,255,255 };
81 extern char key_to_ascii(int keycode )
85 shifted = keycode & KEY_SHIFTED;
92 return shifted_ascii_table[keycode];
94 return ascii_table[keycode];
98 void key_putkey (unsigned short keycode)
100 // this function simulates a key stroke entered
104 temp = key_data.keytail+1;
105 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
106 if (temp!=key_data.keyhead)
108 key_data.keybuffer[key_data.keytail] = keycode;
109 key_data.keytail = temp;
114 void key_clear_bios_buffer_all()
116 // Clear keyboard buffer...
117 *(ushort *)0x41a=*(ushort *)0x41c;
118 // Clear the status bits...
119 // *(ubyte *)0x417 = 0;
120 // *(ubyte *)0x418 = 0;
123 void key_clear_bios_buffer()
125 // Clear keyboard buffer...
126 *(ushort *)0x41a=*(ushort *)0x41c;
136 // Clear the BIOS buffer
137 key_clear_bios_buffer();
139 key_data.keyhead = key_data.keytail = 0;
141 //Clear the keyboard buffer
142 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
143 key_data.keybuffer[i] = 0;
144 key_data.time_pressed[i] = 0;
147 //Clear the keyboard array
149 CurTime =timer_get_fixed_secondsX();
151 for (i=0; i<256; i++ ) {
153 key_data.TimeKeyWentDown[i] = CurTime;
154 key_data.TimeKeyHeldDown[i] = 0;
155 key_data.NumDowns[i]=0;
156 key_data.NumUps[i]=0;
164 if ( n >= KEY_BUFFER_SIZE ) n=0;
168 // Returns 1 if character waiting... 0 otherwise
171 int is_one_waiting = 0;
175 key_clear_bios_buffer();
177 if (key_data.keytail!=key_data.keyhead)
180 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)
205 key_clear_bios_buffer();
207 if (key_data.keytail!=key_data.keyhead) {
208 key = key_data.keybuffer[key_data.keyhead];
209 *time = key_data.time_pressed[key_data.keyhead];
210 key_data.keyhead = add_one(key_data.keyhead);
224 key_clear_bios_buffer();
226 if (key_data.keytail!=key_data.keyhead) {
227 key = key_data.keybuffer[key_data.keyhead];
233 // If not installed, uses BIOS and returns getch();
234 // Else returns pending key (or waits for one if none waiting).
242 while (!key_checkch())
247 unsigned int key_get_shift_status()
249 unsigned int shift_status = 0;
253 key_clear_bios_buffer();
255 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
256 shift_status |= KEY_SHIFTED;
258 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
259 shift_status |= KEY_ALTED;
261 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
262 shift_status |= KEY_CTRLED;
265 if (keyd_pressed[KEY_DELETE])
266 shift_status |=KEY_DEBUGGED;
274 // Returns the number of seconds this key has been down since last call.
275 fix key_down_time(int scancode) {
278 if ((scancode<0)|| (scancode>255)) return 0;
281 if (keyd_editor_mode && key_get_shift_status() )
287 if ( !keyd_pressed[scancode] ) {
288 time_down = key_data.TimeKeyHeldDown[scancode];
289 key_data.TimeKeyHeldDown[scancode] = 0;
291 time = timer_get_fixed_secondsX();
292 time_down = time - key_data.TimeKeyWentDown[scancode];
293 key_data.TimeKeyWentDown[scancode] = time;
300 // Returns number of times key has went from up to down since last call.
301 unsigned int key_down_count(int scancode) {
304 if ((scancode<0)|| (scancode>255)) return 0;
307 n = key_data.NumDowns[scancode];
308 key_data.NumDowns[scancode] = 0;
315 // Returns number of times key has went from down to up since last call.
316 unsigned int key_up_count(int scancode) {
319 if ((scancode<0)|| (scancode>255)) return 0;
322 n = key_data.NumUps[scancode];
323 key_data.NumUps[scancode] = 0;
329 // Use intrinsic forms so that we stay in the locked interrup code.
332 #pragma aux Int5 = "int 5";
334 #pragma off (check_stack)
335 void __interrupt __far key_handler()
337 unsigned char scancode, breakbit, temp;
338 unsigned short keycode;
342 ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
343 if ( ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
344 ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
345 ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
347 _chain_intr( key_data.prev_int_9 );
352 scancode = inp( 0x60 );
356 key_data.E0Flag = 0x80;
359 // Parse scancode and break bit
360 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
362 if ( scancode == 0x1D ) {
363 scancode = KEY_PAUSE;
365 } else if ( scancode == 0x9d ) {
366 scancode = KEY_PAUSE;
369 break; // skip this keycode
371 } else if ( scancode==0xE1 ) {
375 breakbit = scancode & 0x80; // Get make/break bit
376 scancode &= 0x7f; // Strip make/break bit off of scancode
377 scancode |= key_data.E0Flag; // Add in extended key code
379 key_data.E0Flag = 0; // Clear extended key code
383 keyd_last_released = scancode;
384 keyd_pressed[scancode] = 0;
385 key_data.NumUps[scancode]++;
387 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
388 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
389 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
391 temp |= keyd_pressed[KEY_DELETE];
392 if ( !(keyd_editor_mode && temp) )
393 #endif // NOTICE LINK TO ABOVE IF!!!!
394 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
397 keyd_last_pressed = scancode;
398 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
399 if (!keyd_pressed[scancode]) {
401 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
402 keyd_pressed[scancode] = 1;
403 key_data.NumDowns[scancode]++;
405 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) ) {
406 keyd_pressed[KEY_LSHIFT] = 0;
410 } else if (!keyd_repeat) {
411 // Don't buffer repeating key if repeat mode is off
415 if ( scancode!=0xAA ) {
418 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
419 keycode |= KEY_SHIFTED;
421 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
422 keycode |= KEY_ALTED;
424 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
425 keycode |= KEY_CTRLED;
428 if ( keyd_pressed[KEY_DELETE] )
429 keycode |= KEY_DEBUGGED;
432 temp = key_data.keytail+1;
433 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
435 if (temp!=key_data.keyhead) {
436 key_data.keybuffer[key_data.keytail] = keycode;
437 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
438 key_data.keytail = temp;
445 #ifdef PASS_KEYS_TO_BIOS
446 _chain_intr( key_data.prev_int_9 );
450 temp = inp(0x61); // Get current port 61h state
451 temp |= 0x80; // Turn on bit 7 to signal clear keybrd
452 outp( 0x61, temp ); // Send to port
453 temp &= 0x7f; // Turn off bit 7 to signal break
454 outp( 0x61, temp ); // Send to port
455 outp( 0x20, 0x20 ); // Reset interrupt controller
458 #pragma on (check_stack)
460 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
463 ubyte Status1,Status2;
469 Status1= *(ubyte*)0x417;
470 Status2= *(ubyte*)0x418;
472 keyd_time_when_last_pressed = timer_get_fixed_seconds();
473 keyd_buffer_type = 1;
475 key_data.in_key_handler = 0;
479 // Clear the keyboard array
482 if (Installed) return;
485 //--------------- lock everything for the virtal memory ----------------------------------
486 if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler)) {
487 Error( "Can't lock keyboard handler!\n" );
489 if (!dpmi_lock_region (&key_data, sizeof(keyboard))) {
490 Error( "Can't lock keyboard handler's data!\n" );
492 if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char))) {
493 Error( "Can't lock keyboard handler's data!\n" );
495 if (!dpmi_lock_region (&keyd_repeat, sizeof(char))) {
496 Error( "Can't lock keyboard handler's data!\n" );
498 if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char))) {
499 Error( "Can't lock keyboard handler's data!\n" );
501 if (!dpmi_lock_region (&keyd_last_pressed, sizeof(char))) {
502 Error( "Can't lock keyboard handler's data!\n" );
504 if (!dpmi_lock_region (&keyd_last_released, sizeof(char))) {
505 Error( "Can't lock keyboard handler's data!\n" );
507 if (!dpmi_lock_region (&keyd_pressed, sizeof(char)*256)) {
508 Error( "Can't lock keyboard handler's data!\n" );
510 if (!dpmi_lock_region (&keyd_time_when_last_pressed, sizeof(int))) {
511 Error( "Can't lock keyboard handler's data!\n" );
514 key_data.prev_int_9 = _dos_getvect( 9 );
515 _dos_setvect( 9, key_handler );
522 if (!Installed) return;
525 _dos_setvect( 9, key_data.prev_int_9 );
528 key_clear_bios_buffer_all();
531 *(ubyte*)0x417=Status1;
532 *(ubyte*)0x418=Status2;