954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading Keypresses without a "return"

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.

lappy512
Newbie Poster
16 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

thanks!

lappy512
Newbie Poster
16 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

>im using GCC under linux.

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

lappy512
Newbie Poster
16 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You