need a function that woks a little bit like getch

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 13
Reputation: uu666 is an unknown quantity at this point 
Solved Threads: 0
uu666 uu666 is offline Offline
Newbie Poster

need a function that woks a little bit like getch

 
0
  #1
Dec 1st, 2006
Hello! here is what I wanna do:
I have a while() statement, and inside of it I have some instructions to repeat. My problem is that, I want somehow to stop that loop ONLY when I press a key... I it's kinda like getch() but getch() waits for a keypress.... Any help is appreciated Thanx in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: need a function that woks a little bit like getch

 
0
  #2
Dec 1st, 2006
Create a thread which will have only getchar. If something pressed then set a bool value in that thread. Of course in while loop will break when the bool value is set.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: need a function that woks a little bit like getch

 
0
  #3
Dec 2nd, 2006
Originally Posted by uu666 View Post
Hello! here is what I wanna do:
I have a while() statement, and inside of it I have some instructions to repeat. My problem is that, I want somehow to stop that loop ONLY when I press a key... I it's kinda like getch() but getch() waits for a keypress.... Any help is appreciated Thanx in advance.
I assume you have getch() available. Not all compilers do, making any code using it non-portable. But if you do, look to see if the compiler has a function called kbhit(). That's the function you're looking for.
Last edited by WaltP; Dec 2nd, 2006 at 2:01 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need a function that woks a little bit like getch

 
0
  #4
Dec 2nd, 2006
> somehow to stop that loop ONLY when I press a key
You need to say which OS and compiler you're using.
There is no portable answer to this question, so we need specifics.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: uu666 is an unknown quantity at this point 
Solved Threads: 0
uu666 uu666 is offline Offline
Newbie Poster

Re: need a function that woks a little bit like getch

 
0
  #5
Dec 2nd, 2006
i tried with kbhit() but isnt actually what i was looking for... im using borland c++ 3.1.

i have a for loop and i want that when the "h" key is pressed to do something.... however i dont wanna cease program's execution to wait for that key like getch() does.......
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need a function that woks a little bit like getch

 
1
  #6
Dec 2nd, 2006
> i tried with kbhit() but isnt actually what i was looking for
Did you try
  1. if ( kbhit() ) {
  2. ch = getch();
  3. }
kbhit() doesn't do any reading, it only tells you if there's something there.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: uu666 is an unknown quantity at this point 
Solved Threads: 0
uu666 uu666 is offline Offline
Newbie Poster

Re: need a function that woks a little bit like getch

 
0
  #7
Dec 2nd, 2006
Thanx alot dude I owe you
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 58
Reputation: amishosh is an unknown quantity at this point 
Solved Threads: 0
amishosh amishosh is offline Offline
Junior Poster in Training

Re: need a function that woks a little bit like getch

 
0
  #8
Jan 22nd, 2007
Originally Posted by Salem View Post
> i tried with kbhit() but isnt actually what i was looking for
Did you try
  1. if ( kbhit() ) {
  2. ch = getch();
  3. }
kbhit() doesn't do any reading, it only tells you if there's something there.
In your above code the kbhit will pick up on the first keystroke. but
  1. ch = getch();
will wait for another stroke, no?

I want to loop until a key is pressed. Then I want the ability to check that key that was pressed without havin to press it again
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 306
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: need a function that woks a little bit like getch

 
0
  #9
Jan 22nd, 2007
It's been a while since I've used kbhit, so don't shoot me i I'm wrong ; but if I remember correct kbhit will only tell you that a key is pressed. By saying ch = getch() you will actually get the key that is pressed, so if you want to loop until a char is pressed just use something like:

  1.  
  2. while (!(kbhit()));
  3. ch = getchar();

regards Niek
Last edited by niek_e; Jan 22nd, 2007 at 11:15 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need a function that woks a little bit like getch

 
0
  #10
Jan 22nd, 2007
> In your above code the kbhit will pick up on the first keystroke.
Read it again!.

kbhit() does NOT read a key, it merely tells you whether there is a key to be read.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC