key press on Linux C++

I'm writing a program in C++ under Linux that needs to wait for user to press a key. Considering the key pressed the program will go accordingly. So I must be able to recover the key code from a variable afterwards.

I will appreciate a good help.
1
2
3
4
5
6
7
char ch;
cin >> ch;

switch( ch )
{
   // ...
}


Is this what you what? This of course requires the user to press ENTER before the program sees the keystroke.

If you need to act immediately without waiting for ENTER, then you have to force stdin to be unbuffered (which I can explain how if you need).
My bad.... without the Enter, of course. Actually is a small tree menu, with 4-5 options, and enter after every key is not too professional. It would be fantastic if you would also sugest some site/doc/manual where these specs are available. What is free on the net is extremely shallow. No info on how to use keyboard, mouse, soundcard or any other periferal in C. If you would be so kind to point me in the right direction, I will highly appreciate.

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main()
{
    struct termios oldSettings, newSettings;

    tcgetattr( fileno( stdin ), &oldSettings );
    newSettings = oldSettings;
    newSettings.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr( fileno( stdin ), TCSANOW, &newSettings );    

    while ( 1 )
    {
        fd_set set;
        struct timeval tv;

        tv.tv_sec = 10;
        tv.tv_usec = 0;

        FD_ZERO( &set );
        FD_SET( fileno( stdin ), &set );

        int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );

        if( res > 0 )
        {
            char c;
            printf( "Input available\n" );
            read( fileno( stdin ), &c, 1 );
        }
        else if( res < 0 )
        {
            perror( "select error" );
            break;
        }
        else
        {
            printf( "Select timeout\n" );
        }
    }

    tcsetattr( fileno( stdin ), TCSANOW, &oldSettings );
    return 0;
}

It works, I can use it for my purpose. It would be great if you wold also tell me where I can get some details/explanations (like the functions and variables used in this code), other than termios docs feeded by google. It would be nice for me to understand what I'm using :)
I wrote this code several years ago, so I don't remember my sources other than the man pages and linux kernel source. (Hence why I kept the code.) Sorry.
See also
Keep the console open long enough to see your program's output
http://www.cplusplus.com/forum/articles/7312/page1.html#msg33734
(scroll down for the POSIX stuff)

For more information on dinking with the terminal, see
http://www.lafn.org/~dave/linux/terminalIO.html

Also Advanced Programming in the UNIX Environment by W. Richard Stevens
http://www.yendor.com/programming/unix/apue/ch11.html

And
http://www.advancedlinuxprogramming.com/alp-folder

Finally,
http://unixwiz.net/techtips/termios-vmin-vtime.html
http://www.unixguide.net/unix/programming/3.6.2.shtml


For dealing with select(), see
The Unix Programming FAQ
http://www.lib.ru/UNIXFAQ/unixprogrfaq.txt

The Linux Programmer's Manual
http://www.tin.org/bin/man.cgi?section=2&topic=select

Hope this helps.
Topic archived. No new replies allowed.