•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,680 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,185 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 263 | Replies: 6
![]() |
•
•
Join Date: Jun 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
probably a stupid question, but I haven't been able to find anything.
Is there a istream related function that let me read exactly one keystroke
from the keyboard through cin?
What I need it to do is this:
- remove all characters currently in the input buffer
- block until the user presses a key
- return the ascii code of the key that was pressed
This should work also, if the user just hit the enter key.
I'm probably too stupid to get it right, but the usual get/getline()
functions always block until the user hits enter, even if the user enteres
other keys before, but on the other hand, if the user presses enter only,
getline continues to block and does not return the empty line.
Ideally I would like somethink like this:
1. a menu, where the user can press 1, 2 or 3 (and the program reactiving
immediately without the need for the user to press enter afterwards)
2. a "Press any key to continue" function, that waits for exactly one
keystroke, no matter what key it is.
thank you
probably a stupid question, but I haven't been able to find anything.
Is there a istream related function that let me read exactly one keystroke
from the keyboard through cin?
What I need it to do is this:
- remove all characters currently in the input buffer
- block until the user presses a key
- return the ascii code of the key that was pressed
This should work also, if the user just hit the enter key.
I'm probably too stupid to get it right, but the usual get/getline()
functions always block until the user hits enter, even if the user enteres
other keys before, but on the other hand, if the user presses enter only,
getline continues to block and does not return the empty line.
Ideally I would like somethink like this:
1. a menu, where the user can press 1, 2 or 3 (and the program reactiving
immediately without the need for the user to press enter afterwards)
2. a "Press any key to continue" function, that waits for exactly one
keystroke, no matter what key it is.
thank you
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 386
Reputation:
Rep Power: 1
Solved Threads: 37
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation:
Rep Power: 11
Solved Threads: 184
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation:
Rep Power: 11
Solved Threads: 184
You may already have NCurses installed.
Try the following program to see if you do.
Compile with
The code is valid C code also, so it doesn't matter if you use gcc or g++.
If it complains that it doesn't know what libncurses is, try -lcurses, and if that fails, use
and that should (hopefully) get you everything you need.
Hope this helps.
Try the following program to see if you do.
C++ Syntax (Toggle Plain Text)
// hello.cpp #include <curses.h> int main() { initscr(); raw(); noecho(); nonl(); intrflush( stdscr, FALSE ); keypad( stdscr, TRUE ); mvaddstr( 10, 10, "Hello, world" ); mvaddstr( 11, 10, "Press the 'any' key." ); wgetch( stdscr ); endwin(); return 0; }
g++ -o hello hello.cpp -lncurses.The code is valid C code also, so it doesn't matter if you use gcc or g++.
If it complains that it doesn't know what libncurses is, try -lcurses, and if that fails, use
sudo apt-get install ncurses-devand that should (hopefully) get you everything you need.
Hope this helps.
Last edited by Duoas : Jul 13th, 2008 at 11:36 pm.
I believe there is another alternative which doesn't require any other libraries, it is a unix version of getch(), using termios.h?
Scroll down to the very bottom in the below link...
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Scroll down to the very bottom in the below link...
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Last edited by iamthwee : Jul 14th, 2008 at 2:45 pm.
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- tab completion (C)
- Barcode Readers and Java (Java)
Other Threads in the C++ Forum
- Previous Thread: RESTARTING A laptop
- Next Thread: About LPSTR



Linear Mode