User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2008
Posts: 4
Reputation: amit1701 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amit1701 amit1701 is offline Offline
Newbie Poster

getting character input with one keystroke

  #1  
Jul 13th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: getting character input with one keystroke

  #2  
Jul 13th, 2008
There isn't anything standard that would do that, so you lose any chance of code portability.

What compiler and OS are you using.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 386
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: getting character input with one keystroke

  #3  
Jul 13th, 2008
If you're on Windows: system("PAUSE"); may be what you're looking for, though you can't get the character that was pressed, if that's also what you wanted (wasn't sure).
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: getting character input with one keystroke

  #4  
Jul 13th, 2008
Like iamthwee indicated, there is no standard way to do that. You'll have to do something OS-specific.

I suggest you check out the Curses library.

POSIX: NCurses
Win32: PDCurses

Hope this helps.
Reply With Quote  
Join Date: Jun 2008
Posts: 4
Reputation: amit1701 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amit1701 amit1701 is offline Offline
Newbie Poster

Re: getting character input with one keystroke

  #5  
Jul 13th, 2008
i am using g++ in linux....
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: getting character input with one keystroke

  #6  
Jul 13th, 2008
You may already have NCurses installed.

Try the following program to see if you do.
  1. // hello.cpp
  2.  
  3. #include <curses.h>
  4.  
  5. int main() {
  6. initscr();
  7. raw();
  8. noecho();
  9. nonl();
  10. intrflush( stdscr, FALSE );
  11. keypad( stdscr, TRUE );
  12.  
  13. mvaddstr( 10, 10, "Hello, world" );
  14. mvaddstr( 11, 10, "Press the 'any' key." );
  15.  
  16. wgetch( stdscr );
  17.  
  18. endwin();
  19. return 0;
  20. }
Compile with 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-dev
and that should (hopefully) get you everything you need.

Hope this helps.
Last edited by Duoas : Jul 13th, 2008 at 11:36 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: getting character input with one keystroke

  #7  
Jul 14th, 2008
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
Last edited by iamthwee : Jul 14th, 2008 at 2:45 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC