- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to read a single char from keyboard without hi...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2002 11:48 AM
09-05-2002 11:48 AM
In C programming language, how can I read a single character from the keyboard without waiting for the RETURN key? Also, how can I stop characters from being echoed on the screen as they're typed (Not using stty -echo)?
Thanks to all....
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2002 12:01 PM
09-05-2002 12:01 PM
Re: How to read a single char from keyboard without hitting ENTER
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2002 12:08 PM
09-05-2002 12:08 PM
Re: How to read a single char from keyboard without hitting ENTER
but actually when you use getchar(),
getc(), these functions wait until you hit ENTER, even though they read char by char from the stream, they don't start reading until you hit ENTER... you could try it in a simple code:
#include
int main(void)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2002 12:29 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 08:38 AM
09-06-2002 08:38 AM
Re: How to read a single char from keyboard without hitting ENTER
That's exactly what I was looking for...
I have two additional questions about your code:
1) you didn't include
2) Would you mind explaining me those casts to (void) before some function calls? What are they for?
Again I really appreciate your help...
Thanks a lot...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 10:23 AM
09-06-2002 10:23 AM
Re: How to read a single char from keyboard without hitting ENTER
t.c_lflag &= ~(ICANON | IECHO);
becomes
t.c_lflag &= ~(ICANON | IECHO | ISIG);
this will have the effect that the normal interrupt, quit, ... characters have no effect
and allow you to actually read Cntl-C as a character. By the way, when you understand what that rather cryptic-lookings staements do, you are well on your way to using bitwise operators.
If I can find one handy, I'm sure that I have a finished version of this snippit that does buffer flushing and optional lower/upper case conversion. I'll post that version if I can find it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 11:14 AM
09-06-2002 11:14 AM
Re: How to read a single char from keyboard without hitting ENTER
great help...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 12:51 PM
09-06-2002 12:51 PM
Re: How to read a single char from keyboard without hitting ENTER
e.g.
CH=$(onechar -L)
STAT=${?}
case ${STAT} in
0) echo "You pressed ${CH}"
;;
255) echo "This is an error" >&2
;;
*) echo "The ASCII value pressed is \c"
echo "${STAT} (dec)"
;;
esac
The idea is if 0 is returned, a printable character is output on stdout, a return of 255 indicates an error, any other returns indicates that a non-pritable ASCII character was received and the return value is the decimal equivalent and no character is output to stdout.
If you need to recognize function keys or arrow-keys then you really need to use curses. Man curses for details.
This is onechar.c. Compile it like this:
cc onechar.c -o onechar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 12:53 PM
09-06-2002 12:53 PM
Re: How to read a single char from keyboard without hitting ENTER
cc onechar2.c -o onechar
You can invoke either version as onechar -u for full usage.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 01:54 PM
09-06-2002 01:54 PM
Re: How to read a single char from keyboard without hitting ENTER
print -n "Enter a character --> "
stty raw
readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -raw
print "\nCharacter is $readchar"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2002 11:01 PM
09-06-2002 11:01 PM
Re: How to read a single char from keyboard without hitting ENTER
#include
and use function "getch()" instead of "getchar()".
Hope it will solve ur prob...
Cheers...
Satish.