From: bardiria AT isis DOT curtin DOT edu DOT au (Aaron Ardiri) Subject: HELP: keyboard driver gone funny... To: djgpp AT sun DOT soe DOT clarkson DOT edu Date: Thu, 24 Nov 1994 15:48:19 +0800 (WST) hi all. i'm only really new to djgpp - and i have come accross a slight problem. i have tried to write a *simple* keyboard handler that i could possibly use in a game of some sort using djgpp ( on an IBM ). i have enclosed a copy of : > makefile > demo.c > initkey.c > quitkey.c > keyboard.h > keys.def i can't seem to find my error and i was wondering if any of you guys can help me out here. first of all, the program simple redefines interrupt #9 ( keyboard ), and attempts to process any input from the user and places a boolean value in an array that describes the key. ie: if 1 was read from the keyboard port ( which happens to be escape ), the escape field of the array will be set to TRUE - until the code for the release of this key comes throught ( which is 129 ). i alter this array *inside* the interrupt handler - and try to access it from my main program. in otherwords, i do the following ( just to see if the stupid thing works ): initkey(); /* install the *new* kbd driver */ while( !keys[ESC] ); /* wait until the ESC key is pressed */ quitkey(); /* installs the *old* kbd driver */ the way the keys are defined are *no* problem ( ie the way the array is set up ). the problem is that the while( !keys[ESC] ) instruction loops for a random amount of times and then quits *regardless* of the value stored in the array at offset ESC. what i mean by this is that if i enter in ten or so characters that are not escapes, and then press another non escape character - the program exits - and keys[ESC] is still assigned FALSE!!! ??? what is going on ? i hope some of you guys can help us out here - it is really starting to drive me crazy - and i don't know why. i don't really want comments on why it wont work - i would like to know if some of you guys can actually get the damn thing working.... 'cause normally a lot of people comment - but are wrong. thanks to all those that will try and help me out - ps: i lose access to the internet *very* soon -> like in about two days - so i would appreciate really *quick* replies - thanks to all. i will be back in 'march 95. here are the files : ------------------8<--------------------------------------------------------- 'MAKEFILE.' ------------------8<--------------------------------------------------------- # -------------------------------------------------------------------- # KEYBOARD v1.0 -> djgpp makefile to create the library 'libkey.a' and # the accompanying demo. # # NB: make [enter] <-- make the library, and copy to c:\djgpp\lib # make demo [enter] <-- create the demo executable. # -------------------------------------------------------------------- # -------------------------------------------------------------------- # this part tells the gcc compiler how to make a '.c' file a '.o' file # -------------------------------------------------------------------- .c.o: gcc -c $< # -------------------------------------------------------------------- # the listing of all the '.o' files and the name of the library # -------------------------------------------------------------------- OBJ = initkey.o quitkey.o LIB = libkey.a BIN = c:/djgpp/bin/ # -------------------------------------------------------------------- # create an archive of all the '.o' files -> library 'libkey.a', which # can be accessed using the -lkey option ( before $(OBJ) ). # -------------------------------------------------------------------- main : $(OBJ) ar r $(LIB) $(OBJ) ranlib $(LIB) copy $(LIB) c:\djgpp\lib # -------------------------------------------------------------------- # create the demo executable. # -------------------------------------------------------------------- demo : gcc demo.c -lkey ren a.out demo coff2exe -s $(BIN)go32.exe demo del demo # -------------------------------------------------------------------- # the commands to 'clean' the directory ( of all the '.o' files ) # -------------------------------------------------------------------- clean : del *.o # -------------------------------------------------------------------- ------------------8<--------------------------------------------------------- 'KEYS.DEF' ------------------8<--------------------------------------------------------- #define ESC 1 /* escape key */ #define LCTRL 29 /* left control key */ #define LSHIFT 42 /* left shift key */ #define RSHIFT 54 /* right shift key */ #define LALT 56 /* left alt key */ #define SPACE 57 /* space bar */ #define F1 59 #define F2 60 #define F3 61 #define F4 62 #define F5 63 #define F6 64 #define F7 65 #define F8 66 #define F9 67 #define F10 68 #define KUP 72 /* UP (keypad) */ #define KLEFT 75 /* LEFT (keypad) */ #define KRIGHT 77 /* RIGHT (keypad) */ #define KDOWN 80 /* DOWN (keypad) */ #define F11 87 #define F12 88 #define RCTRL 90 /* right control key */ #define RALT 117 /* right alt key */ #define UP 133 /* UP key */ #define LEFT 136 /* LEFT key */ #define RIGHT 138 /* RIGHT key */ #define DOWN 141 /* DOWN key */ ------------------8<--------------------------------------------------------- 'KEYBOARD.H' ------------------8<--------------------------------------------------------- #include #include #include #include #define FALSE 0 #define TRUE !(FALSE) /*************************************************************** interrupt vectors for keyboard handlers ( old and new ) ***************************************************************/ _go32_dpmi_seginfo oldkey, newkey; /*************************************************************** NB: keys -> 0..88 ( standard keys ) 89..172 ( extended keys -> value = 61 + ) ***************************************************************/ #include "keys.def" /* the codes for the keys */ int keys[173]; int keycount; extern void initkey(); extern void quitkey(); ------------------8<--------------------------------------------------------- 'INITKEY.C' ------------------8<--------------------------------------------------------- #include "keyboard.h" void newkeyhandler() /**************************************************************** this is the *new* keyboard handler. all key strokes will be intercepted here. ****************************************************************/ { static int specialkey; static unsigned char key; asm( " cli " ); asm( " movw $0x0060,%dx inb %dx,%al " ); asm( " movb %%al,%0 " : "=g" (key) : /* no input */ ); if (( key == 224 ) || ( key == 225 )) specialkey = TRUE; if (( keycount == 0 ) && ( key != 224 ) && ( key != 225 )) { if ( key & 0x80 != 0x80 ) keys[ key ] = TRUE; else keys[ key ] = FALSE; keycount = -1; } else if (( keycount == 1 ) && ( specialkey )) { if ( key & 0x80 != 0x80 ) keys[ 61+key ] = TRUE; else keys[ 61+key ] = FALSE; specialkey = FALSE; keycount = -1; } else if ( keycount == 2 ) keycount = -1; keycount++; asm( " movw $0x0020,%dx movb $0x20,%al outb %al,%dx " ); asm( " sti " ); /*-----------------------------------------------*/ /* the following fprintf is for *debugging* only */ /*-----------------------------------------------*/ fprintf( stdout, "key = %d, keys[ESC] = %d, specialkey = %d, keycount = %d\n", key, keys[ESC], specialkey, keycount ); } void initkey() { int i; for( i=0; i < 173; i++ ) keys[i] = FALSE; keycount = 0; _go32_dpmi_get_protected_mode_interrupt_vector( 9, &oldkey ); newkey.pm_offset = (int)newkeyhandler; newkey.pm_selector = _go32_my_cs(); _go32_dpmi_set_protected_mode_interrupt_vector( 9, &newkey ); } ------------------8<--------------------------------------------------------- 'QUITKEY.C' ------------------8<--------------------------------------------------------- #include "keyboard.h" void quitkey() { _go32_dpmi_set_protected_mode_interrupt_vector( 9, &oldkey ); } ------------------8<--------------------------------------------------------- 'DEMO.C' ------------------8<--------------------------------------------------------- #include "keyboard.h" void main() { int i; fprintf( stdout, "press [ESC] to stop demo..\n" ); initkey(); while( !keys[ ESC ] ); quitkey(); } ------------------8<--------------------------------------------------------- i hope that is all the files that i need for the program to compile. oh yeah - i forgot to mention - i am using djgpp v1.12 maint2. hear from most of you soon ... regards, aaron ardiri ( bardiri AT isis DOT curtin DOT edu DOT au ) - ( ardiriaj AT cs DOT curtin DOT edu DOT au ) - ( eardiri AT cc DOT curtin DOT edu DOT au )