Home | Reviews | GUIpedia | Forum | Fun500


agumaWhat\'s wrong with my code
Well 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-267:21 PM

ToddRe:What\'s wrong with my code
Try having it printf the "scancode" to see if the array indices are correct. Also your "getch" doesn't seem to be used anywhere in the program.
2009-02-269:44 PM

Re:What\'s wrong with my code
well main.c includes it and it has a loop and it keeps printing getch until the end of time!!! muahahaha!!!! alright thanks, i'll try printing the scancode values.
2009-02-2610:20 PM

trollyRe:What\'s wrong with my code
in your code : [code] 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; [/code] i see than when the key is released (if (scancode & 0x80) in your code) you don't store the key value, that's correct, but, i see that you store the char in keypress after the if...else, so when you presse the key "A" the code will store "A" in keypress and set keydown to 1 , after, when you release the key, the code will new store a valu to keypress and re set keydown to 1 beceause you put this part of code after the if..else sentence. i think that the problem comme from that. try something like this [code] void keyboard_isr(struct regs *r) { scancode=inportb(0x60); yeskey=1; //a first check if whe pressed an special key switch (scancode) { case 29: //ctrl ctrl=1; yeskey=0; break; case 56: //alt alt=1; yeskey=0; break; case 42: //shift1 shift1=1; yeskey=0; break; case 54: //shift2 shift2=1; yeskey=0; break; case 29+128: //ctrl ctrl=0; yeskey=0; break; case 56+128: //alt alt=0; yeskey=0; break; case 42+128: //shift1 shift1=0; yeskey=0; break; case 54+128: //shift2 shift2=0; yeskey=0; break; default: break; }; //if no , we can parse the code if (yeskey) { // if ctrl+alt+del then halt the system if (ctrl&&alt&&scancode==134+48) { asm("mov $0xff,%eax"); asm("int $0x40"); } else { //if the key is pressed (not released) we can store the value in the buffer if (scancode
2009-02-2711:37 AM

Other


2021 Brandon Cornell