How to get all currently available input with ncurses? [duplicate] - c

I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seem to suggest using ncurses.
How should I go about checking if a key is pressed in C++ using ncurses?
The function getch() provided by ncurses reads a character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().

You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1) {
if (kbhit()) {
printw("Key pressed! It was: %d\n", getch());
refresh();
} else {
printw("No key pressed yet...\n");
refresh();
sleep(1);
}
}
}

Related

How to process KEY_HOME and KEY_END reliably under ncurses

I am struggling to have ncurses generate KEY_HOME or KEY_END events, instead the raw escape sequence is coming through as a sequence of characters.
The following simple C program illustrates the problem:
#define _XOPEN_SOURCE 700
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
void clean(void)
{
echo();
nl();
nocbreak();
endwin();
}
int main(int argc, char *argv[])
{
setvbuf(stderr, NULL, _IONBF, 0);
initscr();
cbreak();
nonl();
noecho();
atexit(clean);
keypad(stdscr, TRUE);
clear();
refresh();
int ch = getch();
if (ch == ERR)
errx(EXIT_FAILURE, "getch");
warnx("read: %x", ch);
halfdelay(1);
while((ch = getch()) != ERR)
{
warnx("read: %x", ch);
}
exit(EXIT_SUCCESS);
}
Compile with -lncurses, and redirect stderr to a log file.
When pressing HOME:
test: read: 1b
test: read: 5b
test: read: 31
test: read: 7e
When pressing UP
test: read: 103
How come HOME and END (and indeed F1 etc.) are not parsed by ncurses into KEY_HOME?
You probably have set TERM to a value which does not match the terminal's behavior. For instance, the linux terminal description has khome=\E[1~ (which corresponds to the example output), while xterm has khome=\E[OH. You can see this using
infocmp linux xterm | grep khome
If the terminal description does not match the actual behavior, ncurses will not match the incoming bytes, and will behave as shown.

Configure key repeat delay to detect if key is being pressed

I am writing a program in C that uses ncurses to check if a key is being pressed. The problem is that there is a key repeat delay.
If I for example hold the key 'a' while in the terminal there is a short delay before 'a' gets repeatedly entered. I want to be able to know if it is being pressed from the point where it is actually pressed.
How do temporarily change this delay to be equal to 0 while in the terminal? I'm currently using Mac OSX 10.10.5.
With ncurses (any curses implementation), you would use getch rather than getchar. The latter is a C standard I/O input function.
Someone suggested Create a function to check for key press in unix using ncurses, which contains an answer worth mentioning. It uses nodelay to eliminate the time normally spent in getch for successive bytes of an escape sequence. In curses, you always have a tradeoff between waiting or not, since an escape sequence may not arrive all in one read operation. The example shown there reports cases when no character is available, and pauses (sleeps) for a short time in that case.
If you only want to see the characters which are read, you could eliminate that pause (but making your program use a lot of CPU time):
#include <ncurses.h>
int kbhit(void)
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1) {
if (kbhit()) {
printw("Key pressed! It was: %d\n", getch());
}
}
}
or (recognizing that there is a tradeoff), use napms to pause a short amount of time, but lessening the CPU time used:
#include <ncurses.h>
int kbhit(void)
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1) {
if (kbhit()) {
printw("Key pressed! It was: %d\n", getch());
} else {
napms(20);
}
}
}
Try this to ignore buffered key repeats:
int key;
if ((key = getch()) != ERR) {
while (getch() == key);
}
In context:
//example.c
#include <ncurses.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int pos_x = 0;
int max_x = 0, max_y = 0;
int key = 0;
int on = 1;
initscr();
noecho();
cbreak();
curs_set(FALSE);
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
getmaxyx(stdscr,max_y,max_x);
while(1) {
clear();
mvprintw(0, 0, "Press, hold and release L-R arrow keys. Press UP/DOWN to toggle function.");
mvprintw(1, 0, "Skip buffered repeats: %s", (on ? "ON" : "OFF"));
mvprintw(2, pos_x, "#");
refresh();
usleep(50000);
getmaxyx(stdscr,max_y,max_x);
key = getch();
// skip buffered repeats
if (on) {
if (key != ERR) {
while (getch() == key);
}
}
//
switch (key) {
case KEY_LEFT:
pos_x += (pos_x > 0 ? -1 : 0); break;
case KEY_RIGHT:
pos_x += (pos_x < max_x - 1 ? 1 : 0); break;
case KEY_UP:
on = 1; break;
case KEY_DOWN:
on = 0; break;
}
}
endwin();
}
Compile and run with gcc example.c -lncurses -oexample && ./example

How do I port this program from conio to curses?

I wrote this simple program on Windows. Since Windows has conio, it worked just fine.
#include <stdio.h>
#include <conio.h>
int main()
{
char input;
for(;;)
{
if(kbhit())
{
input = getch();
printf("%c", input);
}
}
}
Now I want to port it to Linux, and curses/ncurses seems like the right way to do it. How would I accomplish the same using those libraries in place of conio?
#include <stdio.h>
#include <ncurses.h>
int main(int argc, char *argv)
{
char input;
initscr(); // entering ncurses mode
raw(); // CTRL-C and others do not generate signals
noecho(); // pressed symbols wont be printed to screen
cbreak(); // disable line buffering
while (1) {
erase();
mvprintw(1,0, "Enter symbol, please");
input = getch();
mvprintw(2,0, "You have entered %c", input);
getch(); // press any key to continue
}
endwin(); // leaving ncurses mode
return 0;
}
When building your program do not forget to link with ncurses lib (-L lncurses) flag to gcc
gcc -g -o sample sample.c -L lncurses
And here you can see kbhit() implementation for linux.

Ctrl+D doesn't stop application from executing in command window

I've written a program to encrypt a given message by XOR. It works, but It doesn't end. Here is the code.(I have created 3 files):
encrypt.h :
void encrypt(char *message);
message_hider.c:
#include <stdio.h>
#include "encrypt.h"
int main() {
char msg[80];
while (fgets(msg, 80, stdin)){
encrypt(msg);
printf("%s", msg);
}
return 0;
}
encrypt.c :
#include "encrypt.h"
void encrypt(char *message) {
while (*message) {
*message++ ^= 0x1f;
}
}
As I mentioned above, It works. but I can't stop it. When I pressed Ctrl+D to stop it (in cmd) It encrypts it also.(I need this code stop after it encrypt a message). Please explain me about this case.
When I pressed Ctrl+D to stop it (in cmd)
If that's the cmd from Windows you probably want Ctrl+Z.
Ctrl-D is used for the console EOF on Unix systems.
Ctrl-Z is used for the console EOF on Windows systems.
isprint() can help:
#include <stdio.h>
#include <ctype.h>
void encrypt(char *message)
{
while (*message) {
*message = *message ^ 31;
message++;
}
}
int main(void)
{
char msg[80];
while (fgets(msg, 80, stdin) != NULL) {
if (!isprint((unsigned char)*msg)) break;
encrypt(msg);
printf("%s", msg);
}
return 0;
}
Add an exit condition:
if( c < 0x20 ) break;
You may need to add other checks also to support backspace without encoding it...
http://www.asciitable.com/
Just run
$> kill -l
To see the list of signals in Linux. You will not find SIGKILL (Ctrl + D) signal there :(
Ctrl + D is SIGKILL (0) signal in Linux which is not documented anywhere.
Ctrl + Z is for Windows which tell EOF and we need to press "Enter" to close.

Can I make ungetc unblock a blocking fgetc call?

I would like to stuff an 'A' character back into stdin using ungetc on receipt of SIGUSR1. Imagine that I have a good reason for doing this.
When calling foo(), the blocking read in stdin is not interrupted by the ungetc call on receipt of the signal. While I didn't expect this to work as is, I wonder if there is a way to achieve this - does anyone have suggestions?
void handler (int sig)
{
ungetc ('A', stdin);
}
void foo ()
{
signal (SIGUSR1, handler);
while ((key = fgetc (stdin)) != EOF)
{
...
}
}
Rather than try to get ungetc() to unblock a blocking fgetc() call via a signal, perhaps you could try not having fgetc() block to begin with and wait for activity on stdin using select().
By default, the line discipline for a terminal device may work in canonical mode. In this mode, the terminal driver doesn't present the buffer to userspace until the newline is seen (Enter key is pressed).
To accomplish what you want, you can set the terminal into raw (non-canonical) mode by using tcsetattr() to manipulate the termios structure. This should case the blocking call to fgetc() to immediately return the character inserted with ungetc().
void handler(int sig) {
/* I know I shouldn't do this in a signal handler,
* but this is modeled after the OP's code.
*/
ungetc('A', stdin);
}
void wait_for_stdin() {
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(fileno(stdin),&fdset);
select(1, &fdset, NULL, NULL, NULL);
}
void foo () {
int key;
struct termios terminal_settings;
signal(SIGUSR1, handler);
/* set the terminal to raw mode */
tcgetattr(fileno(stdin), &terminal_settings);
terminal_settings.c_lflag &= ~(ECHO|ICANON);
terminal_settings.c_cc[VTIME] = 0;
terminal_settings.c_cc[VMIN] = 0;
tcsetattr(fileno(stdin), TCSANOW, &terminal_settings);
for (;;) {
wait_for_stdin();
key = fgetc(stdin);
/* terminate loop on Ctrl-D */
if (key == 0x04) {
break;
}
if (key != EOF) {
printf("%c\n", key);
}
}
}
NOTE: This code omits error checking for simplicity.
Clearing the ECHO and ICANON flags respectively disables echoing of characters as they are typed and causes read requests to be satisfied directly from the input queue. Setting the values of VTIME and VMIN to zero in the c_cc array causes the read request (fgetc()) to return immediately rather than block; effectively polling stdin. This causes key to get set to EOF so another method for terminating the loop is necessary. Unnecessary polling of stdin is reduced by waiting for activity on stdin using select().
Executing the program, sending a SIGUSR1 signal, and typing
t e s t results in the following output1:
A
t
e
s
t
1) tested on Linux
It is not entirely clear what your goal is, but is this what you are looking for?
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
int handle = 0;
void handler (int sig) {
handle = 1;
}
void foo () {
int key;
signal (SIGUSR1, handler);
while ((key = fgetc (stdin)) != EOF) {
printf("%c\n",key);
if (handle) {
handle = 0;
ungetc('A',stdin);
}
}
}
int main(void) {
printf("PID: %d\n",getpid());
foo();
}
It produces this output:
PID: 8902
test (typed on stdin)
t
A
e
s
t
FILE*s are not async safe.
You cannot operate on a FILE* in a signal handler while someone else also uses that same FILE*. functions you can all in a signal handler is stated here:
http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html . (It might be
different on a windows machine, but still any FILE* are not safe there either.
This is essentially the same as #Jamie's answer, slightly changed to support your desire to process the A before the t, but it's too hard to type code into a comment box, so I've posted this answer separately.
int insert_an_A = 0;
void handler(int sig) { insert_an_A = 1; }
void process_char(char c) { ... }
int main(int argc, char **argv) {
int c;
/* skip signal setup */
while ((c = fgetc(stdin)) != EOF) {
if (insert_an_A) {
process_char('A');
insert_an_A = 0;
}
process_char(c);
}
}
If you want to process an handler received during the call to fgetc that returns EOF, you should also check insert_an_A after exiting the while loop.
Note also that in general the best practice for signal handlers is to set a global variable and return from the handler. Elsewhere in your program, look for that variable changing and react appropriately.

Resources