This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C/C++: Capture characters from standard input without waiting for enter to be pressed
How do you do non-blocking console I/O on Linux in C?
I am attempting to write a program in the C language which is in a constant loop but I need to be able to receive input from the keyboard at the start of each cycle or to continue on if there is no key being pressed.
getchar() and _getch() are insufficient as they both wait for the input. If anybody knows of a function similar to _getch but which doesn't wait for the input it would be much appreciated.
There's no C-standard way to do what you want to do (capture keystrokes from the keyboard without the user hitting enter), you have to go platform specific. <conio.h> should have what you need on Windows, you'd need curses on Linux to get this. (I suspect curses would work on Mac as well, but don't quote me on that)
On Windows kbhit() doesn't wait for any input, it just returns if there is a key pressed at the very instant you make the test. If you need to know what the key was as well, then you can combine this with getch()
something like:
while(countdown++ <= 1000){ // give a second to hit something
if(b=kbhit()) // if the user presses a key
break; // leave the loop early
Sleep(1); // else, sleep for 1 ms and try again
}
if (b != 0) // if something was pressed
printf("The key was %d\n", getch());
Related
I am just playing around with ncurses and so, and I discovered a really weird behaviour.
When i use initscr() from the ncurses lib, and afterwards i use a normal getchar(), then the program terminates after pressing the first key.
The normal getchar() behaviour I would expect, is that i can type (more or less) as long till I press return.
#include "curses.h"
int main()
{
initscr();
getchar();
//endwin();
return 0;
}
Can anyone explain me why this happens?
curses initializes the terminal input to raw mode (which in turn makes the connected stream act as if it is unbuffered), so that curses can detect single-character input. getchar assumes that the input is buffered, so that you would press Enter to end an input line. In raw mode, getchar returns right away, because a read call will find something as soon as you press a key.
Besides endwin, you could use other curses functions for switching back/forth between the terminal's raw/cooked modes (see the manual for reset_shell_mode and reset_prog_mode).
This question already has answers here:
Capture characters from standard input without waiting for enter to be pressed
(21 answers)
Closed 6 years ago.
I'm trying an example from "The C programming language" on Windows with CodeBlock and I don't know how I can get user input without having to press enter. This program counts the number of lines in the input using getchar() and so when I run it "enter" only works as a \n and doesn't allow me to send my input. Is there an alternative way to do it?
EDIT
the problem was with the compiler. Figured it out after looking into it for some time
This is not a problem of the C programming language. It has more to do with the terminal that you are using to interface with the C program.
By default, most terminals buffer the input that you type and only send it to the program when you finish the line by pressing enter. This is what allows you to use the backspace key to correct typing mistakes before they are sent to the C program.
If you are not creating an interactive program, then if you redirect the program output to read from a file instead of from the terminal then getchar() will not have to wait. After all, the input file is all there from the start.
./myprogram.exe < myinputfile.txt
If you want to create an interactive program then the simplest thing you can do is to accept that the input comes one full line at a time. It is what users typing at the terminal will expect.
If you want your application to immediately respond to keypresses then you will need to use something outside the standard C library. For example, in Linux you could use ncurses to create terminal applications that can immediately react to a keypress, including arrow keys and so on.
This question already has answers here:
C non-blocking keyboard input
(11 answers)
Closed 9 years ago.
Let's suppose I'm in a while (1) loop calculating something, would it be possible to quit the whole application by pressing a certain key?
It's a C console application without threads.
I'm pretty sure it's not possible, but I'm a newbie so hey. I can only imagine beeing forced to press a key by some function like _getch() or similar. But then you have to wait until the user presses the key and the calculation cannot run meanwhile.
On Windows systems, there's kbhit() function that is nonblocking and returns true when any key is pressed. So, you could change while(1) to while(!kbhit()), or you could if(kbhit()) c = getch() to read the char without waiting. But this is very crude solution, really..
You could do that by using C "signals".
This is not really difficult to use. Take a look at this wikipedia page. ;)
http://en.wikipedia.org/wiki/C_signal_handling
I am writing a program which has a main menu, with a couple functions which I am allowing the user to access by typing in a number for one menu selections. For that, I am using _getch(). I need to be able to exit the program when the user enters the EOF key, but _getch() does not actually catch the EOF keystroke. I know I can get it to work by using getchar() instead of _getch(), but I would prefer not to have the user be forced to press enter after every function selection.
Is there a way to get _getch() to catch EOF? This is for Windows if it is of any importance, any help is really appreciated, as I have been unable to find anything out there.
The standard C input stream stdio is line buffered, and there is no defined way to forcibly flush it. If you want to avoid this and read from the buffer as every new character is entered, you have to use some OS specific magic, which is what _getch from conio.h tries to do.
There are other implementations out there of getch-type functions, for instance getch from the PDCurses library.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C/C++: Capture characters from standard input without waiting for enter to be pressed
I'm using C-Free 4 Standard on windows 7 and I writing a C program.
I'm using getch() as a function to pause the program, however, the character(s) pressed echos on the screen and it waits for the Enter key to be pressed before moving on (it doesn't look any different than how the scanf works). I tried getche(), and it works fine, although the echo appears.
What could be the problem with the getch() function?
The getch, wgetch, mvgetch and mvwgetch, routines read a character
from the window. In no-delay mode, if no input is waiting, the value
ERR is returned. In delay mode, the program waits until the system
passes text through to the program. Depending on the setting of
cbreak, this is after one character (cbreak mode), or after the first
newline (nocbreak mode). In half-delay mode, the program waits until a
character is typed or the specified timeout has been reached.
More or less the same method is used in Windows. You can use _getch() to get the input character available for the application without buffering.
In fact there are several ways to pause the execution of your program until you enter something, one way to do that is by using getchar() (which is part of the function set of stdio.h and an official standard library), It will do the same effect as getch() (this function is part of the functions set of conio.h library which isn't an official library).
If your problem is that you want to avoid pressing Enter every time you enter a character which is not very clear in your question), then read this: How to avoid press enter with any getchar()