Hi how to access standard input buffer to get 1 and second character from there?
for c++ there is a function named peek() but in c libraries I can find one...

Recommended Answers

All 4 Replies

There's no such feature in the stdio library. The best you can do is extract a character, then return it to the stream with ungetc, but that's probably sufficient for your needs. If you need to unget more than one character, some custom buffering will be required to retain portability.

the thing is I want to check for scan codes, if I press F1 keyboard key then I get ansii character 0 and scan code with some number(first byte is ansii and second scan code...), so if I can read byte by byte then I could check for this

You'll need to be getting raw input to read scan codes anyway, so the stdio buffering mechanism won't even be a part of the picture.

Standard C functions won't work, but One way to do it is to use non-standard conio.h functions. special keys will return one of two values the first time getch() is called, either 0 or 224. The actual key code is returned in the second call to getch() and will be the same number as a normal key. For that reason I just make the code a negative value so that the program knows the difference. Some programmers like to just add 255 to the value returned by the second call to getch().

Of course this will not work if your compiler does not support conio.h. The next best thing would be to use a library such as ncurses, or on MS-Windows use win32 api console functions.

#include <conio.h>

int main()
{
    int key = getch();
    if( key == 0 || key == 224)
    {
        // get special key code and make it negative
        // to distintuish it from normal keys
       key = -getch();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.