Solved

Trap keypress in console.

Posted on 2001-08-19
8
905 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 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