Finding a Substitute for getch() on Linux | C Programming & Linux

  • Thread starter Thread starter i.mehrzad
  • Start date Start date
  • Tags Tags
    Linux
Click For Summary

Discussion Overview

The discussion revolves around finding a substitute for the getch() function in C programming on Linux, particularly focusing on capturing user input without displaying it on the screen. Participants explore various methods and functions available in the Linux environment.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that getch() is a DOS-based command and seeks a Linux equivalent for capturing input without display.
  • Another participant suggests using getchar(), but mentions that for capturing input character by character without waiting for the enter key, terminal settings must be adjusted.
  • A request for clarification on the acronym "K&R" leads to a reference to "The C Programming Language" book, which is suggested as a resource for learning C.
  • A participant explains that using termios is necessary to set the terminal to non-canonical mode for reading keystrokes, indicating that this approach is OS dependent.
  • Another participant provides a link to a resource discussing key press detection in Unix programming.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with no consensus on a single solution. The discussion remains unresolved regarding the best method to substitute getch() on Linux.

Contextual Notes

Participants mention that the solution is OS dependent and that specific terminal settings must be configured, which may not be straightforward for all users.

i.mehrzad
Messages
84
Reaction score
0
I use the linux operating system. And at present programming in C.
The getch() doesn't seem to exist on Linux. I know that it is a DOS based command. What is the substitute for getch() on Linux.


I mean is there any command out there which can take my input without displaying the output. I tried the getpass() command but it doesn't work fine

Can someone please help.
 
Technology news on Phys.org
getchar(), although if you need each character at a time without waiting for the enter key you might have to set the terminal mode.
Remember unix is written in C and C was invented for unix so any good C book (such as K&R) will apply directly to unix/linux
 
Can you please repeat the name of the book. I mean i don't know the acronym
 
http://en.wikipedia.org/wiki/The_C_Programming_Language_(book )
It isn't the easiest intro to programming book, but if you have a background it is a good way to learn C.
 
Last edited by a moderator:
I don't have my example at hand. You have to use termios to set the terminal (tty) to non-canonical mode, read a keystroke, then reset the tty to canonical mode.

In other words it is OS dependent - the example I gave was unix-based. Windows has a call - _getche() as I remember. You can set the tty mode in windows as well.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
3
Views
3K
Replies
6
Views
3K
Replies
10
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 18 ·
Replies
18
Views
3K