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.

Recommended Answers

All 5 Replies

That's implementation dependent. What compiler are you using and on what operating system?

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

thanks!

>im using GCC under linux.

Not my area, but I believe ncurses is a good term to feed Google.

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.

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.