Keyboard waiting for input
Posted: Sat Feb 06, 2010 9:34 am
I've added keyboard support to my simple os, but how would I implement a function like getch (in C) & raw_input (in Python)?
The Place to Start for Operating System Developers
http://f.osdev.org/
Code: Select all
volatile unsigned char GotKey = false;
volatile unsigned char Key = 0;
void MyKeyboardIrqHandler()
{
// Previous key not processed yet?
if(GotKey)
return;
// Handle key events, shifts and stuff like that.
// At this point, the key is processed and you have the key as a character (I'm assuming you already have something like this).
GotKey = true;
Key = processed_key;
}
byte GetNextChar()
{
GotKey = false;
while(GotKey == false);
return Key;
}
The best thing to do would be to process each key as it is pressed. Here's a few reasons why:Creature wrote:Also take care with the "if(GotKey) return", so you don't let the keyboard buffer overflow by skipping too many reads
I think the easiest way to store input would be to use a static buffer (possibly a Ring or circular buffer) so you can set a maximum amount of allowed characters. Using a vector or dynamic memory allocation doesn't seem like a good idea as you'd have to resize everytime a character gets pushed.matio wrote:Could I use a buffer?