Page 1 of 1

Keyboard Handler Problem

Posted: Sat Sep 14, 2002 2:03 am
by Matze2002
Hi,
i write a smal OS, and now i try to write a keyboard Handler in PM Mode. The os boots a C File. Here is my function to get keys from user:

char keyb_get_key( void )
{
int this_key = 0x00;
char string[] = {""};

do
{
    this_key = inportb(0x60);
    if (this_key == 1)
    {
       write("ESC");
       this_key = 0x00;
    }
}
while(this_key != 28);
write("ENTER");
}

If a user press ESC on the keyboard the handler write ESC on the Screen. But how can i clear the keyboard buffer.

Matze

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 7:58 am
by drizzt
There's a good example of a keyboard driver in protected mode in crazybuddha's site.

http://www.crazybuddha.com/keyboard.php

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 10:09 am
by Tim
Matze2002: You don't have a keyboard buffer. You're just reading in scan codes and discarding them.

BTW: a better way to wait for and read a key is like this:

Code: Select all

uint8_t scan;

while (true)
{
    /* Wait for a key to be available */
    while ((in(0x64) & 1) == 0)
        ;

    scan = in(0x60);
    /* Discard key up events */
    if (scan & 0x80)
        continue;

    if (scan > 0 && scan < _countof(keys))
         /* Return the character code for this scan code */
        return keys[scan].normal;
}
An even better way is to handle the keyboard interrupt (IRQ 1) and read key presses there; that way, you don't have to constantly check the keyboard (which is what crazybuddha has done).

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 1:54 pm
by AlanO
which header files allow you to use either one of those examples? i am using GCC and DJGPP

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 1:57 pm
by AlanO
also, how do you compile the
char keyb_get_key( void );

example?? it says:

:kernel_c.c: undefined reference to `_inportb'
:kernel_c.c: undefined reference to `_write'
:kernel_c.c: undefined reference to `_write'
):kernel_c.c: undefined reference to `_inportb'
):kernel_c.c: undefined reference to `_write'
):kernel_c.c: undefined reference to `_write'


any ideas???

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 3:06 pm
by Tim
Since you're writing your own OS, you need to implement all these functions.

For the above:
  • inportb: reads an 8-bit value from an I/O port using the in instruction
  • write: writes a string to the screen

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 4:37 pm
by AlanO
okay i got an write to the screen function, but i don't understand the other one, any examples?

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 8:31 pm
by Schol-R-LEA
The inportb() function is little more than a wrapper around the assembly language instruction,
IN AL, portnumber
which is used to read data from I/O ports.

"OSDEV Guide#3: I/O Access using Inline Assembly" gives a good explanation of how it can be implemented using gcc.

Re:Keyboard Handler Problem

Posted: Sat Sep 14, 2002 8:54 pm
by AlanO
okay that did help a bit, but i decided to rewrite it completly. Is there any good examples of a working C (or using ASM file linked to C) keyboard handlers? Just something to allow user to type, save into variable, allow os to compare variable?

Thanks in advanced

Re:Keyboard Handler Problem

Posted: Sun Sep 15, 2002 4:24 am
by Tim
Scroll up this page. See article two? Click on the link there.