-2

我正在尝试在 c 中创建一个操作系统并在 32 位保护模式下进行程序集。我正在尝试创建一个 scanf() 类型的函数,该函数在按下回车按钮之前获取键盘输入。我有一个基本的键盘 IRQ 处理程序设置,它打印任何输入的内容,我想实现一个 scanf() 函数,但我无法将键盘处理程序的值返回到主内核。

这是键盘处理程序的代码。

unsigned int shift_key = 0;

unsigned int caps_key = 0; 

int counter = 0;

char *sbuf;

int scan = 1;

void keyboard_handler(registers_t regs)
{ 

// monitor_write("handler called");

unsigned char scancode; 
scancode = get_scancode();
if(scancode == 0x2A )     
{   
    shift_key = 1;//Shift key is pressed

    //monitor_write("Shift + ");

}      

else if(scancode == 0xAA)   

{   

     shift_key= 0;//Shift Key is not pressed

    //  monitor_write("Unshift");

}  



else if(scancode == 0x3A && caps_key == 1)     
{   

    caps_key = 0;//Caps key is pressed

//  monitor_write("Shift + ");

}      
else if(scancode == 0x3A && caps_key == 0)     

{   

    caps_key = 1;//Caps key is pressed

//  monitor_write("Shift + ");

}  

//Left arrow    

else if(scancode == 0x4B)

{

    move_cursor_LR(1,0);

}

//right Arrow

else if(scancode == 0x4D)

{

    move_cursor_LR(0,1);

}

else    

{          

    if (shift_key == 1 && caps_key == 0)    

    {   

        // int shiftaltctrl = 1;//Put anything to see what special keys were      pressed

        monitor_put(Kkbdus[scancode]);      

        if (scan == 1 && Kkbdus[scancode] != '\n')

        {

        sbuf[counter] = Kkbdus[scancode];       

        counter++ ;

        }

    }       

    if (caps_key == 1 && shift_key == 0)    

    {   

        // int shiftaltctrl = 1;//Put anything to see what special keys were pressed

        monitor_put(Kkbdus[scancode]);      

        if (scan == 1 && Kkbdus[scancode] != '\n')

        {

        sbuf[counter] = Kkbdus[scancode] ;      

        counter++ ;

        }

    }        

    if(shift_key == 0 && caps_key == 0)

    {    

        monitor_put(kbdus[scancode]); //Prints the character which was pressed         

        if (scan == 1 && kbdus[scancode] != '\n')

        {

        sbuf[counter] = kbdus[scancode];        

        counter++ ;

        }

    }   

    if( caps_key == 1 && shift_key == 1)

    {

        monitor_put(kbdus[scancode]);

        if (scan == 1 && kbdus[scancode] != '\n')

        {

            sbuf[counter] = kbdus[scancode];        

            counter++ ;

        }

    }

    if(scan == 1 && kbdus[scancode] == '\n') 

    {



        scan = 0;

        sbuf[0] = '\0';

    }

    if(kbdus[scancode] == '\t')

    {

        monitor_write(sbuf);

    }   

  }

}

当调用 irq 时,我使用 scan 变量作为布尔值将字符放入数组中。但我无法将它返回到我调用它的主文件中。

4

1 回答 1

0

我想到了这一点,并尝试了一段有效的代码。我所做的是在键盘驱动程序中创建了一个函数,该函数将保留最后输入的字符。当我想获取字符时,我会调用getch();它来返回字符。我scanf();在主文件中创建了 main ,它将收集字符并首先将它们设置为 0 以便相同的字符不会重复,然后它会通过一个 while 循环来验证是否按下了 enter 键。char*如果它不是= 0,则将char添加到a中,如果buffch变量设置为1,如果它设置为0,它将不会打印它。这就是理论。

这是代码:

在键盘驱动程序中:添加了 getch 和 getchter ,它们将分别获取 char 并将其设置为 0。

char getch()
{
    char buf = sbuf; // clone the buffer
    getchter();      // set the sbuf to 0 so that the same char is not repeated
    return buf;      // return buff.
}

void getchter()
{
    sbuf = 0;       // setting sbuf to 0
}

这是位于主内核文件中的 scanf() 函数。

char* scanf() //scanf() is called
{
    char* buff;      //make a buffer
    char key = '/0'; // make a single char buffer
    int i = 0;       //counter
    int n = 100;     // counter the number is the limit of chars to input
    int buffch = 1;
    while(i < n)  //until the max char limit is reached
    {
        buffch = 1;  // buffch used as bool to buffer or not buffer the char we 
        key = getch(); // get the last char typed
        if(key == '\n') // if enter is pressed stop
        {
            break;
        }       
        if(key == '\b') if key is a backspace 
        {
            //! dont buffer this char
            buffch = 0;

            if (i > 0)  // if the count is more than 0 then
            {
                //! go back one char in buffer
                i--;
            }  
        }
        //! only add the char if it is to be buffered
        if (buffch == 1)
        {
            if (key != 0) // insure that it is not a 0 ie if it is repeated it will be = 0 
            {
                buff[i++] = key; // add the char to the buffer
            }
        }

    }
    buff[i] = '/0'; in the last set the string to be null terminated
    return buff; // return the buff
}

我希望这对其他人有帮助:)

于 2015-06-16T10:54:18.883 回答