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.
16 #define WIN32_LEAN_AND_MEAN
30 #define KEY_BUFFER_SIZE 16
32 //-------- Variable accessed by outside functions ---------
33 unsigned char keyd_print_screen=0;
35 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
36 unsigned char keyd_repeat;
37 unsigned char keyd_editor_mode;
38 volatile unsigned char keyd_last_pressed;
39 volatile unsigned char keyd_last_released;
40 volatile unsigned char keyd_pressed[256];
41 volatile int keyd_time_when_last_pressed;
43 typedef struct keyboard {
44 unsigned short keybuffer[KEY_BUFFER_SIZE];
45 fix time_pressed[KEY_BUFFER_SIZE];
46 fix TimeKeyWentDown[256];
47 fix TimeKeyHeldDown[256];
48 unsigned int NumDowns[256];
49 unsigned int NumUps[256];
50 unsigned int keyhead, keytail;
55 static volatile keyboard key_data;
57 static unsigned char Installed=0;
59 unsigned char ascii_table[128] =
60 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
61 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
62 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
63 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
64 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,255,255,
66 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
67 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
68 255,255,255,255,255,255,255,255 };
70 unsigned char shifted_ascii_table[128] =
71 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
72 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
73 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
74 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
75 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,255,255,
77 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
78 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
79 255,255,255,255,255,255,255,255 };
82 // Initialization and Cleanup Routines
86 keyd_time_when_last_pressed = timer_get_fixed_seconds();
92 // Clear the keyboard array
94 if (!Installed) Installed = 1;
100 if (!Installed) return;
101 key_clear_bios_buffer_all();
105 extern char key_to_ascii(int keycode )
109 shifted = keycode & KEY_SHIFTED;
116 return shifted_ascii_table[keycode];
118 return ascii_table[keycode];
121 void key_clear_bios_buffer_all()
126 void key_clear_bios_buffer()
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;
158 keyd_print_screen = 0;
164 if ( n >= KEY_BUFFER_SIZE ) n=0;
168 // Returns 1 if character waiting... 0 otherwise
171 int is_one_waiting = 0;
173 key_clear_bios_buffer();
175 if (key_data.keytail!=key_data.keyhead)
178 return is_one_waiting;
185 key_clear_bios_buffer();
187 if (key_data.keytail!=key_data.keyhead) {
188 key = key_data.keybuffer[key_data.keyhead];
189 key_data.keyhead = add_one(key_data.keyhead);
195 int key_inkey_time(fix * time)
199 key_clear_bios_buffer();
201 if (key_data.keytail!=key_data.keyhead) {
202 key = key_data.keybuffer[key_data.keyhead];
203 *time = key_data.time_pressed[key_data.keyhead];
204 key_data.keyhead = add_one(key_data.keyhead);
211 void key_putkey (unsigned short keycode)
213 // this function simulates a key stroke entered
217 temp = key_data.keytail+1;
218 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
219 if (temp!=key_data.keyhead)
221 key_data.keybuffer[key_data.keytail] = keycode;
222 key_data.keytail = temp;
231 key_clear_bios_buffer();
233 if (key_data.keytail!=key_data.keyhead) {
234 key = key_data.keybuffer[key_data.keyhead];
240 // If not installed, uses BIOS and returns getch();
241 // Else returns pending key (or waits for one if none waiting).
249 while (!key_checkch())
254 unsigned int key_get_shift_status()
256 unsigned int shift_status = 0;
259 key_clear_bios_buffer();
261 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
262 shift_status |= KEY_SHIFTED;
264 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
265 shift_status |= KEY_ALTED;
267 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
268 shift_status |= KEY_CTRLED;
271 if (keyd_pressed[KEY_DELETE])
272 shift_status |=KEY_DEBUGGED;
279 // Returns the number of seconds this key has been down since last call.
280 fix key_down_time(int scancode) {
283 if ((scancode<0)|| (scancode>255)) return 0;
286 if (keyd_editor_mode && key_get_shift_status() )
290 if ( !keyd_pressed[scancode] ) {
291 time_down = key_data.TimeKeyHeldDown[scancode];
292 key_data.TimeKeyHeldDown[scancode] = 0;
294 time = timer_get_fixed_secondsX();
295 time_down = time - key_data.TimeKeyWentDown[scancode];
296 key_data.TimeKeyWentDown[scancode] = time;
302 // Returns number of times key has went from up to down since last call.
303 unsigned int key_down_count(int scancode) {
306 if ((scancode<0)|| (scancode>255)) return 0;
308 n = key_data.NumDowns[scancode];
309 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;
321 n = key_data.NumUps[scancode];
322 key_data.NumUps[scancode] = 0;
327 // Use intrinsic forms so that we stay in the locked interrup code.
330 #pragma aux Int5 = "int 5";
334 // Send keyboard message to Descent keyboard system.
336 void send_key_msg(UINT msg, WPARAM vkeycode, LPARAM keypack)
338 unsigned char scancode, temp;
339 unsigned short keycode;
341 // Extract 8-bit OEM scancode
342 scancode = (char)((keypack >> 16) & 0xff);
344 if (vkeycode == VK_SNAPSHOT) {
345 scancode = KEY_PRINT_SCREEN;
346 keyd_print_screen = 1;
348 else if (keypack & 0x01000000) scancode |= 0x80;
349 if (vkeycode == VK_PAUSE) scancode = KEY_PAUSE;
354 case WM_KEYDOWN: // Keyboard down
355 // Handle Pauses the easy way.
356 keyd_last_pressed = scancode;
357 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
359 if (!keyd_pressed[scancode]) {
360 // First time key is down.
361 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
362 keyd_pressed[scancode] = 1;
363 key_data.NumDowns[scancode]++;
366 if ((keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP)) {
367 keyd_pressed[KEY_LSHIFT] = 0;
372 else if (!keyd_repeat) {
373 // don't buffer repeating key if repeat mode is off
376 if (scancode != 0xaa) {
377 keycode = (unsigned short)scancode;
379 if (keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
380 keycode |= KEY_SHIFTED;
382 if (keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
383 keycode |= KEY_ALTED;
385 if (keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
386 keycode |= KEY_CTRLED;
389 if (keyd_pressed[KEY_DELETE])
390 keycode |= KEY_DEBUGGED;
393 temp = key_data.keytail+1;
394 if (temp >= KEY_BUFFER_SIZE) temp = 0;
396 if (temp!= key_data.keyhead) {
397 key_data.keybuffer[key_data.keytail] = keycode;
398 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
399 key_data.keytail = temp;
405 case WM_KEYUP: // handle key ups
406 keyd_last_released = scancode;
407 keyd_pressed[scancode] = 0;
408 key_data.NumUps[scancode]++;
410 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
411 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
412 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
415 temp |= keyd_pressed[KEY_DELETE];
416 if (!(keyd_editor_mode && temp))
418 // NOTICE LINK TO ABOVE IF!!!!
419 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() -
420 key_data.TimeKeyWentDown[scancode];