We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,111 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Detecting keypresses in an endless loop

I working with an application written in Visual Studio C++. Then program is a console application running under the DOS-prompt. The program is something like this.

void main ()
{
    for ( ;; )
    {
        do_something ();
        do_something_more ()
    }
}

Now I need to add some code between do_something () and do_something_more () that checks if any key on the PC-s keyboard is pressed, and if so read that key. The problem is that the program must continue with do_something_more () if no key is used. I can not wait for the keypress, the program must continue executing as before if no key is pressed. I have not been able to figure out how to do this for this type of application. Anybody can give me a hint of how to do this?

5
Contributors
5
Replies
18 Hours
Discussion Span
7 Months Ago
Last Updated
7
Views
dgipling
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Visual C++ supports kbhit() and getch() in the conio.h library, together they do what you want.

deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

GetAsyncKeyState() can also be used for this purpose. Check this.

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 0

And main() is NOT void. It's int.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

Thanks a lot, this was exactly what I needed.

dgipling
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

And always main() must be int method not void as it returns a value.

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0946 seconds using 2.75MB