clear keyboard buffer - flush STDIN

C 16Bit (MS-DOS) Add comments

I use inportb macro for keyboard port 60h to get keystrokes from the keyboard buffer directly using a loop.
for example in a loop executes and in that block i will use this function to get the scan codes of keystrokes and use it… like the following

	unsigned char key;
		if((key = inportb(0x60))==1)break; // if esc is pressed

i hope people know why %d is used for a char datatype. But behind this there is a contradiction among many young programmers…

here, your screen will be flooded with scan codes whether or not press any key… that’s the use of inportb…
you press a key and you can see the scan code. if you want the ascii code then you have to issue an interrupt to get both scan codes and ascii code as integer in which the first 8bits and other 8bits will give you the ascii and scan codes…

and the problem is when you press a key and before you release you are sending more than the limit of keystrokes to the the keyboard buffer and to notify this the beep sound beeps… and these extra keystrokes will appear for your next input which is not needed… it is like if you press ‘k’ and then you do some process and then your code again waits for the key strokes now you will get the remaining k’s from the keyboard buffer if it is not flushed…

so here comes the following code which will clear the keyboard buffer for every time it executes…

void main()
	int i;
		asm int 21h

whether or not a key is pressed the buffer is cleared… so from the next stroke i could assume that only one entry will be present in the keyboard buffer before it is read…

and hope people are good in registers of the processor and interrupts to understand the above code which has an inline assembly language code.

21h is for keyboard and other two are sub functions to clear buffer. maybe i should sit a little more to elaborate. déjame ver if i could… .

Leave a Reply

Entries RSS