C - Read directly from the keyboard buffer

This is a question in the C programming language.

How to read data directly in the keyboard buffer?

I want to access data directly and store it in a variable. What data type should the variable be?

I need this for the operating system that our institute is developing. It is called ICS-OS, and I'm not quite sure about the specifics. It runs on x86, 32-bit machines (we run it on QEMU in a Linux box). Here is the link for the Google code http://code.google.com/p/ics-os/ . Hope enough information.

The operating system does not support the conio.h library, so kbhit is not an option.

+4
source share
4 answers

It depends on the platform.

If this is for Windows, the most direct access to the "keyboard buffer" is using WM_INPUT and GetRawInputData. See Using source input with a keyboard and mouse example.

Another DOS / Windows way is the conio.h of the getch () / kbhit () function.

The portable library is called Curses and has ports for Linux and Windows.

However, since you are targeting a specific OS, you need to check the documents for that OS.

The most direct platform-independent way is getchar / scanf / everything that is read from stdin, but stdin is a string buffer, so you won’t get any data until the enter key is pressed. You can change the buffering settings, but again, it depends on the platform and may not be possible on any platform. See the related discussion of setbuf (stdin, NULL) .

+8
source

Have you tried looking at the linux kernel source code for the keyboard driver? Take a look at /drivers/input/keyboard/xtkbd.* for a simple XT keyboard driver.

In addition, here is an article that briefly explains how this is done.

+1
source

if you want to read data directly from the keyboard buffer, you can use getchar or getc!

-1
source

This is read from the keyboard buffer.

 scanf("%d",&myvariable); 

but you should use "% d" for int, "% f" for float,% e for double, "% c" for char, "% s" for strings to identify the type that should match the type of your variable.

-3
source

Source: https://habr.com/ru/post/1343885/


All Articles