keyboard driver help

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

keyboard driver help

Post by earlz »

I have tried everything and can not figure out why the keyboard buffer always appears empty to getkey yet is being emptied only when getkey is called and it is getting characters properly

this is my code(sorry for messiness, like i said i have tried everything)

Code: Select all

/*because of how complex a keyboar driver can get, it gets it own file*/
/*for lower case*/
inline unsigned char inportb(unsigned int port);
unsigned char kbdus[128] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
  '9', '0', '-', '=', '\b',	/* Backspace */
  '\t',			/* Tab */
  'q', 'w', 'e', 'r',	/* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
    0,			/* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
 '\'', '`',   0,		/* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
  'm', ',', '.', '/',   0,				/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};

unsigned char kbduscaps[128] =
{
    0,  27, '!', '@', '#', '$', '%', '^', '&', '*',	/* 9 */
  '(', ')', '_', '+', '\b',	/* Backspace */
  '\t',			/* Tab */
  'Q', 'W', 'E', 'R',	/* 19 */
  'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',	/* Enter key */
    0,			/* 29   - Control */
  'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',	/* 39 */
 '"', '~',   0,		/* Left shift */
 '|', 'Z', 'X', 'C', 'V', 'B', 'N',			/* 49 */
  'M', '<', '>', '?',   0,				/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};
typedef struct{
	byte caps;
	byte shift;
	byte scroll;
	byte num;
	byte ctrl;
	byte alt;
}shift_locks; /*for simplicity and speed*/
shift_locks shifts;

#define CAPS_LOCK 0x01
#define RSHIFT 0x02
#define LSHIFT 0x04
#define SCROLL_LOCK 0x08
#define NUM_LOCK 0x10
#define CTRL 0x20
#define ALT 0x80
/*not really aranged in any particular order*/

byte keys[128]; /*64 key keyboard buffer*/
byte keycount=0; /*serves as index for keyboard buffer*/
typedef struct{
	byte asc;
	byte scan;
}sasc;


/* Handles the keyboard interrupt */
void keyboard_handler(void)
{
    unsigned char scancode;

    /* Read from the keyboard's data buffer */
    scancode = inportb(0x60);

    /* If the top bit of the byte we read from the keyboard is
    *  set, that means that a key has just been released */
    if (keycount>=128){return 0;}
    if (scancode & 0x80)
    {
    	switch (scancode){
    		case 42:
    		shifts.shift=0;
    		break;
    		case 54:
    		shifts.shift=0;
    		break;
    		case 29:
    		shifts.ctrl=0;
    		break;
    		case 56:
    		shifts.alt=0;
		break;
	}
    	/*
    	if (scancode==42){ /*left shift*
    		shifts=shifts & 0xFB;}
    	if (scancode==54){ /*right shift*
    		shifts=shifts &0xFD;}
    	if (scancode==29){ /*control*
    		shifts=shifts & 0xDF;}
    	if (scancode==56){/*alt*
    		shifts=shifts & 0x7F;}
    	if (scancode==58){ /*caps lock*
    		shifts=shifts & 0xFE;}
    	if (scancode==69){ /*num lock*
    		shifts=shifts & 0xEF;}
    	if (scancode==70){ /*scroll lock*
    		shifts=shifts & 0xF7;}
    	*/
        /* You can use this one to see if the user released the
        *  shift, alt, or control keys... */
    }
    else
    {
        /* Here, a key was just pressed. Please note that if you
        *  hold a key down, you will get repeated key press
        *  interrupts. */

        /* Just to show you how this works, we simply translate
        *  the keyboard scancode into an ASCII value, and then
        *  display it to the screen. You can get creative and
        *  use some flags to see if a shift is pressed and use a
        *  different layout, or you can add another 128 entries
        *  to the above layout to correspond to 'shift' being
        *  held. If shift is held using the larger lookup table,
        *  you would add 128 to the scancode when you look for it */
 	if (scancode==58){
 		/*set lights*/
 		shifts.caps=shifts.caps ^ 1;
 	}
 	if (scancode==69){
 		
		shifts.num=shifts.num ^ 1;
	}
	if (scancode==70){
		
		shifts.scroll=shifts.scroll ^ 1;
	}       
	
       if (shifts.caps==1 ^ shifts.shift==1){
       	keys[keycount]=kbduscaps[scancode];
       	keycount++;
       	keys[keycount]=scancode;
       	keycount++;
       }else{
       	keys[keycount]=kbdus[scancode];
       	printc(0,0,keys[keycount]);
       	if (keys[keycount]=='h'){printf(&keys);}
       	keycount++;
       	keys[keycount]=scancode;
       	keycount++;
       }
    }
}

void key_back(void){
	/*does stuff while waiting for a key*/
}


sasc getkey(void){
	sasc ret_key;byte *k;
	k=&keycount;
	while(keycount<1){
		/*background*/
	}

	keycount--;
	ret_key.scan=keys[keycount];
	keycount--;
	ret_key.asc=keys[keycount];
	

	
	return ret_key;
}
/*and my calling code*/


z=getkey();printc(0,1,z.asc);printf("yay");
/*it never gets to the "yay"*/
i have been working on this for a while and finally decided i have no idea whats wrong, everything seems to work except for getkey
Last edited by earlz on Thu Sep 22, 2005 11:00 pm, edited 1 time in total.
Post Reply