Solved

Trap keypress in console.

Posted on 2001-08-19
8
904 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 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