![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
[Tutorial] How to hide characters from being displayed on screen as they are keyed in?As has been mentioned, standard C keyboard input routines (getc, fgets, scanf, etc.) buffer the input until you press "Enter" --- and they echo each character as its key is pressed.
Old-style DOS functions like getch() are still available in certain Windows command-line application compilers (such as Borland C, for example). getch() returns the character to the calling program but doesn't echo it to the screen. Here is a routine that I picked up somewhere some time ago (sorry, I didn't record the source, but I am quite sure I didn't delete any copyright notice). If it turns out that it is copyrighted or patented or something: let me know. I compiled and executed it on my Linux system, and also on my windows system (using CYGWIN gcc). Output looks the same. Here's the function, called linux_getch(): CPP / C++ / C Code:
Here's a test program for linux_getch(): CPP / C++ / C Code:
Here's how I compiled them: gcc -o test_getch test_getch.c linux_getch.c Regards, Dave |
|
#2
|
|||
|
|||
|
Thank you Dave, this is exactly what I need.
Stefaan. |
|
#3
|
|||
|
|||
Re: [Tutorial] How to hide characters from being displayed on screen as they are keyed in?Dave,
I found your post of linux_getch.c through a google search. It works great on linux. For some reason on solaris it works in batches of 4 keypresses. For example using your test program, I have to press 4 keys before anything is printed (and then the output of all 4 keys is printed). I also need to press the control-D key 4 times before it is recognized on solaris. Do you know why it behaves this way on solaris? Frank |
|
#4
|
|||
|
|||
Re: [Tutorial] How to hide characters from being displayed on screen as they are keyeQuote:
This is the problem whenever one gets away from C standard library functions. Everything is implementation dependent. If your Solaris installation includes ncurses, you could investigate using that. Regards, Dave |
|
#5
|
|||
|
|||
Re: [Tutorial] How to hide characters from being displayed on screen as they are keyed in?Dave,
I found the problem and a solution. The termio value of MIN is set to 4 on solaris and set to 1 on linux. Adding one line makes it work on both platforms. After this line: newstuff.c_lflag &= ~(ICANON | ECHO); /* reset "canonical" and "echo" flags*/ add this line: newstuff.c_cc[VMIN] = 1; Thanks for your program and I hope this info will be helpful for some who work on multiple plarforms. Frank |
|
#6
|
|||
|
|||
Re: [Tutorial] How to hide characters from being displayed on screen as they are keyeQuote:
Great feedback! That's the main reason I keep coming here: I always learn something (always). Thanks for posting. Regards, Dave |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The