GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Aug-2004, 18:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

[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:
#include <stdio.h>
#include <termios.h> 
#include <unistd.h>

#define END_FILE_CHARACTER 0x04  /* ctrl-d is unix-style eof input key*/


int linux_getch(void) 
{
  struct termios oldstuff;
  struct termios newstuff;
  int    inch;
  
  tcgetattr(STDIN_FILENO, &oldstuff);
  newstuff = oldstuff;                  /* save old attributes               */
  newstuff.c_lflag &= ~(ICANON | ECHO); /* reset "canonical" and "echo" flags*/
  tcsetattr(STDIN_FILENO, TCSANOW, &newstuff); /* set new attributes         */
  inch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldstuff); /* restore old attributes     */

  if (inch == END_FILE_CHARACTER) {
    inch = EOF;
  }
  return inch;
}




Here's a test program for linux_getch():

CPP / C++ / C Code:
#include <stdio.h>
int linux_getch(void);

int main()
{
  int kb_char;

  printf("\n\n  This is a test of a function that (more-or-less)\n");
  printf("  emulates the classic DOS \"getch()\" function.\n\n");
  printf("  The keyboard input is not echoed; each character is\n");
  printf("  is returned to the calling program as a key is pressed.\n\n");
  printf("  For test purposes, this program echos the hex value returned\n");
  printf("  from the keyboard input routine.\n\n");
  printf("Press any key(s)");
  printf(" (the keyboard routine is set so that ctlr-d is EOF).\n\n");
  while ((kb_char = linux_getch()) != EOF) {
    printf("%02X ", kb_char);
  }
  printf("\n\n  Function linux_getch() says that you entered EOF\n\n");
  return 0;
}



Here's how I compiled them:

gcc -o test_getch test_getch.c linux_getch.c

Regards,

Dave
  #2  
Old 20-Jun-2005, 09:08
svhb svhb is offline
New Member
 
Join Date: Jun 2005
Posts: 11
svhb is on a distinguished road
Thank you Dave, this is exactly what I need.

Stefaan.
  #3  
Old 08-Sep-2006, 07:58
Frank Pittenger Frank Pittenger is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Frank Pittenger is on a distinguished road

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  
Old 08-Sep-2006, 08:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: [Tutorial] How to hide characters from being displayed on screen as they are keye


Quote:
Originally Posted by Frank Pittenger
Do you know why it behaves this way on solaris?
I'm sorry but I have no way to test (or fix) it on Solaris installations. I was answering a specific question that was posted on gidforums asking how someone could get the equivalent to Borland getch() on Linux.

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  
Old 08-Sep-2006, 13:29
Frank Pittenger Frank Pittenger is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Frank Pittenger is on a distinguished road

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  
Old 08-Sep-2006, 14:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: [Tutorial] How to hide characters from being displayed on screen as they are keye


Quote:
Originally Posted by Frank Pittenger
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.
.
.
add this line:
newstuff.c_cc[VMIN] = 1;


Great feedback! That's the main reason I keep coming here: I always learn something (always).

Thanks for posting.

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.