Page 1 of 1

Problem with shift key

Posted: Sat Oct 29, 2005 11:48 am
by JimShorts
I downloaded Bran's tutorial and was modifying it like crazy until it wasn't Bran's tutorial anymore ;) Now I'm working on implementing the shift keys. The keys get detected as being pressed and a debug message prints, but nothing happens when they get released. (The caps lock that just won't turn off...) Here's the relevant part of my KB code:

Code: Select all

    bool SHIFT_PRESSED = 0;
// ...
    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 (scancode & 0x80)
    {
        /* You can use this one to see if the user released the
        *  shift, alt, or control keys... */
      if (scancode == 42 || scancode == 54) //42 and 54 are left/right shift scancodes
      {
         SHIFT_PRESSED = 0;
         puts("debug: Shift released"); //this never gets printed
      }
    }
   if (scancode == 42 || scancode == 54)
   {
      //then it was shift
      SHIFT_PRESSED = 1;
      puts("debug: Shift pressed"); //this gets printed
   }
else
//normal keypress code - removed from this post for shortness

Re:Problem with shift key

Posted: Sat Oct 29, 2005 12:49 pm
by JimShorts
Fixed. But now if I try to type a lowercase letter, it will print both the lowercase and capital letters. Intentional capital letters work fine.

Code: Select all

void keyboard_handler(struct regs *r)
{
    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 (scancode == (42|0x80) || scancode == (54|0x80))
    {
      SHIFT_PRESSED = 0;
    }
   else if (scancode == 42 || scancode == 54)
   {
      //then it was shift
      SHIFT_PRESSED = 1;
   }
    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 (SHIFT_PRESSED)
      {
         putch(kbdus_shift[scancode]);
      }
      else
      {
         putch(kbdus[scancode]);
      }
    }
}

Re:Problem with shift key

Posted: Sat Oct 29, 2005 2:31 pm
by JimShorts
Also fixed.

Re:Problem with shift key

Posted: Sat Oct 29, 2005 3:13 pm
by Warrior
Uh, HTH? ;)