• C

Two easy questions

I have a couple questions about simple C stuff:

1) How do I pause the computer for x seconds?

2) How, at a given point in the program, do I test whether a key, any key, has been pressed?

Please provide specific code.


Mark Ebden
Mark EbdenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1. This is OS specific.
   for Dos: sleep(n), where n: sec
            or delay(n),where n: msec
  for Windows Sleep(n), where n: msec
2. Same.
   For dos: getch() or
    bioskey(): see Doc
 for windows: trap
  WM_CHAR message and test uKey

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Or , fore Windows you can  use
Api for test some specific key.
Mark EbdenAuthor Commented:
Re: Question 1: What library are sleep and delay in?

Re: Question 2: Please reread the original question.
If you are still stuck, keep in mind the following: I want to test, "noninvasively", whether a key has been pressed at or before that point.  Please provide specific code.

Thank you,

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

For sleep/delay you must use
#include <dos.h>. Example:
/* delay example */

/* emits a 440-Hz tone for 500 milliseconds */
#include <dos.h>

int main(void)
   return 0;
2. For pression: bioskey:
/* bioskey example */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT  0x01
#define LEFT   0x02
#define CTRL   0x04
#define ALT    0x08

int main(void)
   int key, modifiers;

   /* function 1 returns 0 until a key is pressed */
   while (bioskey(1) == 0);

   /* function 0 returns the key that is waiting */
   key = bioskey(0);

   /* use function 2 to determine if shift keys were used */
   modifiers = bioskey(2);
   if (modifiers)

      if (modifiers & RIGHT) printf("RIGHT");
      if (modifiers & LEFT)  printf("LEFT");
      if (modifiers & CTRL)  printf("CTRL");
      if (modifiers & ALT)   printf("ALT");
   /* print out the character read */
   if (isalnum(key & 0xFF))
      printf("'%c'\n", key);
      printf("%#02x\n", key);
   return 0;

Mark EbdenAuthor Commented:
Thank you - that's what I was looking for.  I still can't get this to work, though.  I am writing regular C code within Microsoft's Developer Studio, and it doesn't recognize bios.h, and won't find sleep or delay within dos.h.  Unfortunately, I don't have the help CD that goes with Developer Studio, so I can't figure out where Sleep (Visual C++ version) is.  

Which library is Visual C++ Sleep in?


Or , Developer Studio is Windows(not Dos)! You must use Sleep() and
GetKeaboardState/GetKeyState/GetAsyncKeyState Api, as I wrote in an answer.
Mark EbdenAuthor Commented:
But Developer Studio doesn't like it when I use Sleep().  Please provide specific code.


For using Sleep/SleepEx you must
set flag MultiThread in Developer
Studio. How exectly i don't know,
(i use BC), but of course it is very
Mark EbdenAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.