Page 1 of 1

Keyboard Problems

Posted: Fri May 05, 2006 11:00 pm
by ua
Hi everyone,

I'm having problems with this code:

Code: Select all

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

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

    if (scancode & 0x80)
    {
 
        if (scancode==42) { shift=0; }  // check values of shift, ctrl and alt
        if (scancode==54) { shift=0; }
        if (scancode==29) { ctrl=0; }
        if (scancode==56) { alt=0; }

    }
    else
    {
        /* Here, a key was just pressed. */

        if (scancode==42) { shift=1; }  // check values of shift, ctrl and alt
        if (scancode==54) { shift=1; }
        if (scancode==29) { ctrl=1; }
        if (scancode==56) { alt=1; }
        if (alt!=1) {                  // keypresses do not count if alt or ctrl are
            if (ctrl!=1) {            // being held down during the keypress.
                 if (shift==1) {                   
                       if (showkeypresses==1) {  // use caps if shift is held down
                                putch(kbdcaps[scancode]);
                           }
                 } else {
                       if (showkeypresses==1) {  // otherwise use normal lowercase letters
                            putch(kbd[scancode]); }
                 }
    }
}
}
}
It's supposed to:

Code: Select all

If a key was released:
  check if the released key was SHIFT, CTRL, or ALT
  if so, set the it's variable to 0.
If a key was pressed:
  check if SHIFT, CTRL, or ALT were pressed.  if so:
     set the key's variable to 1.
  otherwise:
    if shift is pressed display using the caps layout
    if alt is pressed display nothing at all
    if ctrl is pressed display nothing at all
However, when I press and hold SHIFT to type an exclamation mark, for example, when I let go of SHIFT the computer acts as if the shift key is still on (anything after the shift keypress uses the caps layout).

The same happens for the CTRL and ALT keys - if I press one of those the input is disabled until a reboot.

Why might this be?

Re: Keyboard Problems

Posted: Fri May 05, 2006 11:00 pm
by lemon
after the check is complete, reset all the keys' variables to 0

Re: Keyboard Problems

Posted: Fri May 05, 2006 11:00 pm
by ua
Which check?

Re: Keyboard Problems

Posted: Fri May 05, 2006 11:00 pm
by ua
Oh, I understand now - I think.

Re: Keyboard Problems

Posted: Sun May 07, 2006 11:00 pm
by rexlunae
Look at this code for a moment:
uberaaz wrote:

Code: Select all

    if (scancode & 0x80)
    {
 
        if (scancode==42) { shift=0; }  // check values of shift, ctrl and alt
        if (scancode==54) { shift=0; }
        if (scancode==29) { ctrl=0; }
        if (scancode==56) { alt=0; }

    }
...
Nothing in this block of code will ever run, because in the outside if, you check the high bit, which, since scancode is unsigned is effectively a check to see if scancode >= 128. Inside the if block, however, all your comparisons are against values < 128, so none of them will ever run. Since this is the only place you clear your shift, ctrl, or alt flags, they cannot be cleared once set.

The other bit of code is too badly indented for me to look at. You might want to clean it up a bit so it's easier to understand. Also, you might consider a switch statement, instead of a set of nested ifs.