Hello Daniweb Forum Users,
I have been trying to create a ported version of a caculator game to C, but I am new to C and have been learning from "The C Programming Language", 2nd Edition.

I have had only one major trouble that has prevented me from starting: I cannot find a function to directly get the Keypress of the keyboard, without waiting for the user to press enter.

Any Ideas?
Thanks.

im using GCC under linux.
I have found a function for a windows enviroment, but i have not found that yet.

thanks!

ncurses looks like it might work, i will be reading into it. If anyone else knows a solution, please post it!
Thanks Dave.

echo(), noecho(), cbreak(), nocbreak(), raw() & noraw() are the functions in ncurses you are looking for. If you are windowing with ncurses also read about keypad (WINDOW, bool)

You can also do the same thing for terminals, but it's a little more convoluted than ncurses and it's the library I prefer.

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
 
struct termios initial_settings, new_settings;
 
tcgetattr (fileno (input), &initial_settings);
new_settings = initial_settings;
newsettings.c_lflag &= ~ICANON;
newsettings.c_lflag &= ~ECHO;
newsettings.c_cc[VMIN] = 1;
newsettings.c_cc[VTIME] = 0;
tcsetattr (fileno (input), TCSANOW, &new_settings);

now using a terminal you'll receive characters in an uncooked mode. It's a real pain to deal with function and special keys though.

This article has been dead for over six months. Start a new discussion instead.