Solved

Trap keypress in console.

Posted on 2001-08-19
8
833 Views
Last Modified: 2010-05-18
I need a method that waits x seconds for a user to press a key. If they key is pressed, it returns the character. If no key is pressed after x seconds, the method returns some non-character value (-1 perhaps?).

This method should run in a DOS application, not a windows application (the project I need the method for is intended to be run from AUTOEXEC.BAT).

I've attempted writing this myself but I'll be damned if I can work out how to do it. Every input command/method I've tried requires the user to press a key *and then press Enter*. This is no good to me; I want the program to wait for a single keystroke and if none is given to carry on anyway (with a different result).

100 points in anticipation of a quick answer; I get bored quickly and will give up if this takes too long.
0
Comment
Question by:Subhuman
  • 4
  • 3
8 Comments
 

Expert Comment

by:beyonddeath
ID: 6403310
Id recommend looping for x seconds and each time through the loop call kbhit() .. if it is true then theres a key waiting... if not and your loop ends... then no key was hit... if you do get a kbhit... and wanna know what it was... use getch().. it wont wait because you know there is somthing there...

Simple... ps .. I sure hope this wasnt homework ;-)
0
 

Accepted Solution

by:
beyonddeath earned 100 total points
ID: 6403319
Im bored .. ill write the code;p
int x = 5; // seconds to wait
char got = -1;
time_t = time(NULL);
for(;time(NULL)-time_t <= x ; )
{
 if(kbhit)
{
got = getch();
break;
}
}


0
 
LVL 1

Author Comment

by:Subhuman
ID: 6403321
Heh, nope. If it was homework it'd be in Java and I wouldn't need to ask for something this simple. :)

kbhit() ... What header to I need to include for that? Makes it easier finding the docs for it if I know where to look. :)
0
 

Expert Comment

by:beyonddeath
ID: 6403329
conio.h ;p... (i cant beleive i can actually answer one of these questions)... that is in vc++ 6
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:beyonddeath
ID: 6403330
and as i check turbo c++... so itll work in dos...
0
 
LVL 1

Author Comment

by:Subhuman
ID: 6403331
Ooh, thanks. :) Missed that before posting.

*copies and pastes into notepad for testing on other computer*
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6403438
additional comment:

to correctly support extended key codes (like functions keys etc) you should use:

int got = -1;  // use int, not char
...
if(kbhit)
{
got = getch();
if (!got) {
  // extended key code "waiting" in keyboard buffer
  got = getch() << 8;
}
break;
}


Peter

0
 
LVL 1

Author Comment

by:Subhuman
ID: 6404549
Thanks, works like a charm. :)

Thanks for the tip too, peterchen. :)

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now