Keyboard Problems

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
ua
Posts: 17
Joined: Mon Jul 25, 2005 11:00 pm

Keyboard Problems

Post 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?
Last edited by ua on Fri May 05, 2006 11:00 pm, edited 1 time in total.
lemon
Posts: 4
Joined: Thu Mar 02, 2006 12:00 am

Re: Keyboard Problems

Post by lemon »

after the check is complete, reset all the keys' variables to 0
ua
Posts: 17
Joined: Mon Jul 25, 2005 11:00 pm

Re: Keyboard Problems

Post by ua »

Which check?
ua
Posts: 17
Joined: Mon Jul 25, 2005 11:00 pm

Re: Keyboard Problems

Post by ua »

Oh, I understand now - I think.
rexlunae
Member
Member
Posts: 134
Joined: Sun Oct 24, 2004 11:00 pm
Location: North Dakota, where the buffalo roam

Re: Keyboard Problems

Post 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.
Last edited by rexlunae on Sun May 07, 2006 11:00 pm, edited 1 time in total.
Post Reply