aguma | What\'s wrong with my codeWell this is supposed to be a keyboard handler, but whenever I type stuff it just spits out random characters. (Like 'a' would be '' and 'm' would be ' ')
Can you figure out what's wrong with it?
Thanks!
(I'm pretty new at this, so don't think it's so amazingly totally obvious)
[code]
#include
int shift_state = 0;
unsigned char keypress;
int keydown;
unsigned char kbd[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 kbd_shift [128] =
{
/* Shift key codes */
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 */
};
/* Handles the keyboard interrupt */
void keyboard_handler(struct regs *r)
{
unsigned char scancode,new_char;
//keypress = "";
/* Read from the keyboard's data buffer */
scancode = inportb(0x60);
switch(scancode) {
case 0x2a:
shift_state = 1;
break;
case 54:
shift_state=1;
break;
case 0xaa:
shift_state = 0;
break;
case 128+54:
shift_state=0;
break;
default:
if (scancode & 0x80) {
// Ignore it
} else {
if (shift_state==1) {
new_char = kbd_shift[scancode];
} else {
new_char = kbd[scancode];
}
/* Do something with new_char. */
keypress = new_char;
keydown = 1;
}
break;
}
outportb(0x20,0x20);
}
unsigned char getch ()
{
unsigned char p;
keydown = 0;
keypress = "";
while (keydown!=1) {
puts (""); // For some reason, GCC gets rid of the loop otherwise, so I have to put this here.
p = keypress;
}
keydown = 0;
keypress = "";
return p;
}
/* Installs the keyboard handler into IRQ1 */
void keyboard_install()
{
irq_install_handler(1, keyboard_handler);
}
[/code] | 2009-02-26 | 7:21 PM |