Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trap keypress in console.

Posted on 2001-08-19
8
Medium Priority
?
910 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.
Suggested Courses

618 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