Solved

Trap keypress in console.

Posted on 2001-08-19
8
907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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