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.

Recommended Answers

All 5 Replies

There are several ways to accomplish that -- functions in stdio.h or iostream. For example: cin.get()

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.

How about using NCURSES and then getch() ;

Posted lately sorry,

I haven't done this in awhile but the need to hit enter is a function of the shell not the C function. The shell fills the input buffer and holds onto it until an enter is pressed. Then it releases it to the C function. So you need to alter the function of the shell. You do this with stty in your startup script. You need to set your shell to raw. This will change a number of things so be careful. The shell is not like dos. In the shell you re running inside and interrupter, such as bash ( it's not command.com ). That's why shell scripts are so much more powerful the bat files. So checkout the man page for stty.

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.