Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I read characters from a terminal without requiring the user to hit RETURN?

0
Posted

How do I read characters from a terminal without requiring the user to hit RETURN?

0

Check out cbreak mode in BSD, ~ICANON mode in SysV. If you don’t want to tackle setting the terminal parameters yourself (using the “ioctl(2)” system call) you can let the stty program do the work – but this is slow and inefficient, and you should change the code to do it right some time: #include main() { int c; printf(“Hit any character to continue\n”); /* * ioctl() would be better here; only lazy * programmers do it this way: */ system(“/bin/stty cbreak”); /* or “stty raw” */ c = getchar(); system(“/bin/stty -cbreak”); printf(“Thank you for typing %c.\n”, c); exit(0); } Several people have sent me various more correct solutions to this problem. I’m sorry that I’m not including any of them here, because they really are beyond the scope of this list. You might like to check out the documentation for the “curses” library of portable screen functions. Often if you’re interested in single-character I/O like this, you’re also interested in doing some sort of screen display control, and th

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123